Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,21 @@ Of course, there are some differences which will be obvious to the user if they
## Supported Databases ##

- MySQL
- SQLite
- PostgreSQL
- SQLite

## Features ##

- Finder methods
- Dynamic finder methods
- Writer methods
- Relationships
- Validations
- Callbacks
- Model Caching
- Database adapter plugins
- Finder methods, static and dynamic
- Relationships
- Serializations (json/xml)
- Transactions
- Support for multiple adapters
- Validations
- Writer methods
- Miscellaneous options such as: aliased/protected/accessible attributes
- Model Caching

## Installation ##

Expand Down Expand Up @@ -150,4 +149,4 @@ echo $post->title; # 'New real title'

## Contributing ##

Please refer to [CONTRIBUTING.md](https://github.com/jpfuentes2/php-activerecord/blob/master/CONTRIBUTING.md) for information on how to contribute to PHP ActiveRecord.
Please refer to [CONTRIBUTING.md](https://github.com/php-activerecord/activerecord/blob/master/CONTRIBUTING.md) for information on how to contribute to PHP ActiveRecord.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"scripts": {
"style-check" : "php vendor/bin/php-cs-fixer fix --dry-run --verbose --diff",
"style-fix" : "php vendor/bin/php-cs-fixer fix --verbose",
"test": "phpunit",
"test": [
"Composer\\Config::disableProcessTimeout",
"phpunit"
],
"stan": "phpstan analyse --ansi --memory-limit 256M"
}
}
5 changes: 3 additions & 2 deletions lib/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ public function __construct(array $attributes = [], bool $guard_attributes = tru
public function &__get($name)
{
// check for getter
$name = strtolower($name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be something like:

$methodName = "get_" . strtolower($name) ;
if (method_exists($methodName) {
            $retValue = $this->$name();
            return $retValue;

...that way you don't have to worry about stuff breaking if you make it down to line 358, right? (maybe we should add tests around non-lowercase attribute names?)

Copy link
Contributor

@shmax shmax Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, fun TODO: make the casing configurable (eg. I use camelCase in my own codebase, not snake_case).

Edit: when I say TODO I mean for some far-off day, not this PR!

if (method_exists($this, "get_$name")) {
$name = "get_$name";
$res = call_user_func([$this, $name]);
Expand Down Expand Up @@ -479,11 +480,11 @@ public function __isset($attribute_name)
*/
public function __set(string $name, mixed $value): void
{
$name = strtolower($name);
if (array_key_exists($name, static::$alias_attribute)) {
$name = static::$alias_attribute[$name];
} elseif (method_exists($this, "set_$name")) {
$name = "set_$name";

$this->$name($value);

return;
Expand Down Expand Up @@ -946,7 +947,7 @@ private function insert($validate = true)
// if we've got an autoincrementing/sequenced pk set it
// don't need this check until the day comes that we decide to support composite pks
// if (count($pk) == 1)

$pk = strtolower($pk);
$column = $table->get_column_by_inflected_name($pk);

if ($column->auto_increment || $use_sequence) {
Expand Down
2 changes: 0 additions & 2 deletions test/ActiveRecordFindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public function test_find_returns_single_model() {
$author = Author::find(["name"=>"Bill Clinton"]);
$this->assertInstanceOf(Model::class, $author );

$author = Author::find(["name"=>"Bill Clinton"]);
$this->assertInstanceOf(Model::class, $author );

$author = Author::find_by_name("Bill Clinton");
$this->assertInstanceOf(Model::class, $author );
Expand Down
13 changes: 13 additions & 0 deletions test/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,19 @@ public function test_setter()
$this->assertEquals(md5('plaintext'), $author->encrypted_password);
}

public function test_case_insensitivity()
{
$author = new Author();
$author->mixedCaseField = 'Peter';
$this->assertEquals('Peter', $author->mixedcasefield);

$author->MIXEDCASEFIELD = 'Paul';
$this->assertEquals('Paul', $author->mixedCaseFIELD);

$author->mixedcasefield = 'Simon';
$this->assertEquals('Simon', $author->MIXEDCASEFIELD);
}

public function test_setter_with_same_name_as_an_attribute()
{
$author = new Author();
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/authors.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
author_id,parent_author_id,name
1,3,"Tito"
2,2,"George W. Bush"
3,1,"Bill Clinton"
4,2,"Uncle Bob"
author_id,parent_author_id,name,mixedCaseField
1,3,"Tito","Tito"
2,2,"George W. Bush","George W."
3,1,"Bill Clinton","Bill"
4,2,"Uncle Bob","Uncle Bob"
8 changes: 4 additions & 4 deletions test/models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ class Book extends Model
public static $has_one = [];
public function upper_name()
{
return strtoupper($this->name); // keep?
return strtoupper($this->name);
}

public function name()
{
return strtolower($this->name); // keep
return strtolower($this->name);
}

public function get_publisher()
{
return strtoupper($this->read_attribute('publisher')); // keep
return strtoupper($this->read_attribute('publisher'));
}

public function get_upper_name()
{
return strtoupper($this->name); // keep
return strtoupper($this->name);
}

public function get_lower_name()
Expand Down
4 changes: 2 additions & 2 deletions test/sql/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ CREATE TABLE authors(
author_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
parent_author_id INT,
name VARCHAR(25) NOT NULL DEFAULT 'default_name',
mixedCaseField varchar(50),
updated_at datetime,
created_at datetime,
some_Date date,
some_time time,
some_text text,
some_enum enum('a','b','c'),
encrypted_password varchar(50),
mixedCaseField varchar(50)
encrypted_password varchar(50)
) ENGINE=InnoDB;

CREATE TABLE honest_lawyers (
Expand Down
4 changes: 2 additions & 2 deletions test/sql/pgsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ CREATE TABLE authors(
author_id SERIAL PRIMARY KEY,
parent_author_id INT,
name VARCHAR(25) NOT NULL DEFAULT 'default_name',
"mixedCaseField" varchar(50),
updated_at timestamp,
created_at timestamp,
"some_Date" date,
some_time time,
some_text text,
encrypted_password varchar(50),
"mixedCaseField" varchar(50)
encrypted_password varchar(50)
);

CREATE TABLE honest_lawyers (
Expand Down
4 changes: 2 additions & 2 deletions test/sql/sqlite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ CREATE TABLE authors(
author_id INTEGER NOT NULL PRIMARY KEY,
parent_author_id INT,
name VARCHAR (25) NOT NULL DEFAULT default_name, -- don't touch those spaces
mixedCaseField varchar(50),
updated_at datetime,
created_at datetime,
some_Date date,
some_time time,
some_text text,
encrypted_password varchar(50),
mixedCaseField varchar(50)
encrypted_password varchar(50)
);

CREATE TABLE honest_lawyers(
Expand Down