Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM Relationships not saving the Foreign Key
  • I have an issue with ORM relationships. I find_one() of a supplier, then I add in a person and an address. This person and address are correctly saved to their respective tables, and the extra supplier information is also saved. Yet the person.id and address.id are not saved in the supplier table. Supplier migration - http://scrp.at/aHr
    Supplier create table - http://scrp.at/aHk
    PHP to save addresses / people to a supplier - http://scrp.at/aHl Models (edit, these extend class Model extends Orm\Model {})
    Suppler - http://scrp.at/aHn
    Person - http://scrp.at/aHo
    Address - http://scrp.at/aHp The debug of the supplier->address_factory and supplier after the save - http://scrp.at/aHq I have read through a similar issue (http://fuelphp.com/forums/topics/view/1661) including Jelmer's suggestion (I was originally get the stdclass issue described). I have also set the foreign keys to be null as discussed further on. Complete stuck on this problem now.
  • There's 2 issues here at least: 1. You're using find_one_by() which the Orm doesn't support, it's either find_by_ (= one) or find_all_by_, and for PKs you can just use find($id) 2. You're using a has_one relation for something that's a belongs_to, when the foreign key is in the table that table belongs to the other one. This is the case for all your has-one relations btw, which don't fail miserably because you've set the keys to be used manually - though that only makes the fails less understandable as it can't work.
  • Damn. So simple! Thank-you. I have swapped the $_belongs_to and $_has_one around in the models and this is now saving the foreign keys correct. I have fixed the find_one_by() which was generated by the scaffolding but was using Model_Crud (I really think the APIs should be the same). -- When I try to save the model and all the relationships again, I get the following error - "Orm\FrozenObject [ Error ]: No changes allowed." which you mention has been fixed in <a href="https://github.com/fuel/orm/issues/117">Issue 177 on GitHub</a>. I switched to the 1.1/develop branch and get a different issue. On the initial save I get the following error "OutOfBoundsException [ Error ]: Unknown property or relation: supplier_factory"
  • I switched to the 1.1/develop branch and get a different issue. On the initial save I get the following error "OutOfBoundsException [ Error ]: Unknown property or relation: supplier_factory"
    That happens when you try to use a non-existant key or when the relations are being set using a non-existant key. Based on the models you're posting you're probably attempting to fetch that key from the wrong model.
  • Checked it and I made a mistake in my previous fix, should really be fixed now.
  • Just to be clear - newbie question - is the code generated by scaffolding not to be used as a guide? Why the differences? Thanks, Ben.
  • In the latest version Scaffolding uses Model_Crud instead of the ORM by default, though you can still make it create Orm models.
  • Thanks; to scaffold with ORM, I've used the following commands to create a basic scaffold: oil g scaffold freak title:string summary:varchar[250] body:text --orm and ran the migration. Tried the basic listing http://$sitename/freaks and got this error:
    "Fuel\Core\FuelException [ Error ]: Listing columns failed, you have to set the model properties with a static $_properties setting in the model. Original exception: Database method Fuel\Core\Database_PDO_Connection::list_columns is not supported by Fuel\Core\Database_PDO_Connection" So I specified all the field names in the Freak model: protected static $_properties = array('id', 'title', 'summary', 'body', 'created_at', 'updated_at'); And now get the following error:
    Fuel\Core\Database_Exception [ Error ]: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.' in 'where clause' with query: "SELECT `t0`.`id` AS `t0_c0`, `t0`.`title` AS `t0_c1`, `t0`.`summary` AS `t0_c2`, `t0`.`body` AS `t0_c3`, `t0`.`created_at` AS `t0_c4`, `t0`.`updated_at` AS `t0_c5` FROM `freaks` AS `t0` WHERE `t0`.`id` = '0' OR ((`t0`.`` IS null)) LIMIT 1" "OR ((`t0`.`` IS null)) " seems to be the problem. This is with no code hacking, straight out of the box.
  • Scratch that; just read the Troubleshooting section again and am trying "mysql" in my db connection string...
  • ...which didn't work. Gets me straight back to the error: ... Unknown column 't0.' in 'where clause' ...
  • ...which didn't work. Gets me straight back to the error: ... Unknown column 't0.' in 'where clause' ...
  • Update; I've zeroed and reinstalled the default fuel application from scratch, copied and pasted the oil+orm scaffolding code from the docs page: oil g scaffold post title:varchar[50] body:text user_id:int --orm ...and still get the same ... Unknown column 't0.' in 'where clause' ... error: when I try to view http://.../posts Fuel\Core\Database_Exception [ Error ]: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.' in 'where clause' with query: "SELECT `t0`.`title` AS `t0_c0`, `t0`.`body` AS `t0_c1`, `t0`.`user_id` AS `t0_c2` FROM `posts` AS `t0` WHERE `t0`.`id` = '0' OR ((`t0`.`` IS null)) LIMIT 1" COREPATH/classes/database/mysql/connection.php @ line 210 The backtrace is as follows: COREPATH/classes/database/query.php @ line 240
    PKGPATH/orm/classes/query.php @ line 901
    PKGPATH/orm/classes/query.php @ line 959
    PKGPATH/orm/classes/model.php @ line 424
    PKGPATH/orm/classes/model.php @ line 545
    APPPATH/classes/controller/frocks.php @ line 8
    APPPATH/classes/controller/frocks.php @ line 8
    COREPATH/classes/request.php @ line 443
    DOCROOT/index.php @ line 38 the "Post" model specifies:
    protected static $_properties = array('title', 'body', 'user_id'); I've also tried:
    protected static $_properties = array('id', 'title', 'body', 'user_id');
    and
    protected static $_properties = array('id', 'title', 'body', 'user_id', 'updated_at', 'created_at'); with effectively identical results. My config/DEVELOPMENT/db.php is as follows:
    'connection' => array(
    'dsn' => 'mysql:host=localhost;dbname='mypersonal_fuel_dbname',
    'username' => 'mypersonal_fuel_dbuser',
    'password' => 'mypersonal_fuel_password', I've also tried specifying 'mysql' (and database name) instead of 'pdo' in the main config/db.php file. The results are indistinguishable. My stack is snowleopard+MAMP, php version 5.3, with plenty of other non-fuel php sites virtually hosted. Help greatly appreciated. -Ben.
  • We're going wildly of the original topic here, this is more of a post for Oil right now. This looks like the error you might get when using the Model_Crud templates with the Orm, thus I think this is a problem with Oil. That's not really my area of expertise to say the least.
  • I shall repost. Thanks for the help so far. Redirecting to: http://fuelphp.com/forums/topics/view/6082

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion