flightphp / active-record Goto Github PK
View Code? Open in Web Editor NEWMicro Active Record library in PHP, supports chain calls, events, and relations.
Home Page: https://docs.flightphp.com/awesome-plugins/active-record
License: MIT License
Micro Active Record library in PHP, supports chain calls, events, and relations.
Home Page: https://docs.flightphp.com/awesome-plugins/active-record
License: MIT License
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;
After insert()
, the PK changes to become the table row number. I don't think this is intended behaviour right?
$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...
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...
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?
There's a lot of variables that don't need to be included when print_r()/var_dump() etc.
In some cases, you need to view the generated sql for debugging.
Thanks!
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');
}
}
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){}
Add jsonSerializeable to the class so that this is easy to use in an API. Likely serialize the data, and customData parameters.
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. 🤣
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.