Giter Club home page Giter Club logo

active-record's People

Contributors

billtec avatar lloydzhou avatar n0nag0n avatar shangshj avatar victorruan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

active-record's Issues

jsonSerializeable

Add jsonSerializeable to the class so that this is easy to use in an API. Likely serialize the data, and customData parameters.

Bug: isHydrated() remains true even after reset()

I think isHydrated() should be cleared when we reset()

$user->find();
echo $user->isHydrated() ? "Yes" : "No";  // Yes
echo $user->name;  // "John Doe"
$user->reset();
echo $user->isHydrated() ? "Yes" : "No";  // Yes
var_dump($user->name);  // NULL
var_dump($user->toArray());  // array(0){}

Suggestion: Add a table_name parameter to WHERE conditions

I love the where conditions because it makes the query code so much easier to read. A single authorisation check joins 3 tables with conditions on two fields, sort order, blah blah.. Such a pain to read (and debug).

I wanted to ->eq("other_table.field", "value") but it becomes mytable.other_table.field in SQL .. So I have to write the ->where() by hand.. ha ha..

So if ->eq() and friends can either have an optional third param being the table name, or become smart enough not to insert the table name automatically when it has already been specified.. that would eliminate another big use of ->where() and ->pdo->quote() ...

Yes, I am a spoiled brat. 🤣

Something Strange with Custom Primary Key

I'm having some strange problems when using a custom PK... Not too sure where it is, but my gut feel is two bugs are related somehow.

Assume a DB with:

CREATE TABLE my_table (my_pk TEXT NOT NULL PRIMARY KEY, data INTEGER, name TEXT)

Then try something like this

$db = new \flight\ActiveRecord($pdo, "my_table", ["primaryKey" => "my_pk"]);
echo "IsHydrated: " . ($db->isHydrated() ? "Yes" : "No") . PHP_EOL;  // No

$my_pk = time();

$db->my_pk = $my_pk;
$db->data: = 12345;
echo "PK: " . $db->my_pk . PHP_EOL;

$db->save();  // Doesn't Work
echo "IsHydrated: " . ($db->isHydrated() ? "Yes" : "No") . PHP_EOL;  // No
echo "PK: " . $db->my_pk . PHP_EOL;  // PK

$db->insert();  // Now it really gets inserted
echo "IsHydrated: " . ($db->isHydrated() ? "Yes" : "No") . PHP_EOL;  // No
echo "PK: " . $db->my_pk . PHP_EOL;  // This is Row No, not PK

$db->find($my_pk);  // This works
echo "IsHydrated: " . ($db->isHydrated() ? "Yes" : "No") . PHP_EOL;  // Yes
echo "PK: " . $db->my_pk . PHP_EOL;  // Back to PK instead of Row No

$db->name = "Boo";
echo "IsDirty: " . ($db->isDirty() ? "Yes" : "No") . PHP_EOL;  // Yes
$db->save();
echo "IsDirty: " . ($db->isDirty() ? "Yes" : "No") . PHP_EOL;  // No
echo "IsHydrated: " . ($db->isHydrated() ? "Yes" : "No") . PHP_EOL;  // Yes
echo PHP_EOL;

Strange Issue 1

After insert(), the PK changes to become the table row number. I don't think this is intended behaviour right?

Strange Issue 2

$db->save() is supposed to either insert() or update() depending on the situation right? But it doesn't do anything for "inserts" in this case, so you have to specifically insert()...

Can also try:

$db = new \flight\ActiveRecord($pdo, "my_table", ["primaryKey" => "my_pk"]);
$db->my_pk = "Duplicate ID";
$db->save();  // No exception
$db->insert();  // Exception

I would have thought that an exception would have occurred on the save() ..

I have a feeling that these two problems are related somehow. Maybe there's something I missed, so I'm still investigating...

Additional Discussion

After insert(), isHydrated() continues to say no. I think it is fine - documentation states that it is true only if a find() was successful.

I'm perfectly OK with this behaviour, but just wanted to ask if it should also be true after a successful insert() ? Does a successful "insert" also imply a successful "find" in an ORM pattern like ActiveRecord because the model remains with the record data? Note that after "updating/saving" a previously "found" record, isHydrated() is still true... so maybe people might misunderstand and try to use isHydrated() as a form of hasData() when it is not really ?

Ha ha.. not trying to start a flame war here...

Support for JSON fields in MySQL and MariaDB

It seems that the JSON_* functions (like JSON_ARRAY, JSON_SEARCH) used to support JSON fields in new version of MySQL anb MariaDB cannot be used in current version of active-record.

Is there any workaround?

Bug: beforeUpdate & beforeSave event is not working

Just curious why the following examples listed in FlightPHP official docs is not working as expected.

beforeSave(ActiveRecord $ActiveRecord)/afterSave(ActiveRecord $ActiveRecord)
This is useful if you want events to happen both when inserts or updates happen. I'll spare you the long explanation, but I'm sure you can guess what it is.

class User extends flight\ActiveRecord {

    public function __construct($database_connection)
    {
        parent::__construct($database_connection, 'users');
    }

    protected function beforeSave(self $self) {
        $self->last_updated = gmdate('Y-m-d H:i:s');
    } 
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.