Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
SQL error with conditional relation ORM query
  • I have two models photos and a promote_photo. Basically users can vote on a photo they like (once). I'm trying to see if the viewing user has promoted a photo but I'm getting an SQL error ONLY when I add conditions to the relation.
    class Model_Photo extends Orm\Model {
    
        protected static $_properties = array('id', 'user_id', 'full', 'med', 'icon', 'map', 'place_id', 'created_at', 'updated_at');
        protected static $_observers = array(
            'Orm\Observer_CreatedAt' => array('before_insert'),
            'Orm\Observer_UpdatedAt' => array('before_save'),
        );
        protected static $_belongs_to = array(
            'user' => array(
                'key_from' => 'user_id',
                'model_to' => 'Model_Profile',
                'key_to' => 'user_id',
                'cascade_save' => false,
                'cascade_delete' => false,
            ),
            'place' => array(
                'key_from' => 'place_id',
                'model_to' => 'Model_Place',
                'key_to' => 'id',
                'cascade_save' => false,
                'cascade_delete' => false,
            ),
            'promoted' => array(
                'key_from' => 'id',
                'model_to' => 'Model_Promote_Photo',
                'key_to' => 'photo_id',
                'cascade_save' => false,
                'cascade_delete' => true,
                ));
    
    }
    
    class Model_Promote_Photo extends Orm\Model {
    
        protected static $_properties = array('id', 'user_id', 'photo_id', 'created_at', 'updated_at');
        protected static $_observers = array(
            'Orm\Observer_CreatedAt' => array('before_insert'),
            'Orm\Observer_UpdatedAt' => array('before_save'),
        );
        protected static $_belongs_to = array(
            'user' => array(
                'key_from' => 'user_id',
                'model_to' => 'Model_Profile',
                'key_to' => 'user_id',
                'cascade_save' => false,
                'cascade_delete' => false,
            ),
            'photo' => array(
                'key_from' => 'photo_id',
                'model_to' => 'Model_Photo',
                'key_to' => 'id',
                'cascade_save' => false,
                'cascade_delete' => false)
            );
            
    
    }
    

    and I'm using the ORM find() to query the database as follows.
     $photos = Model_Photo::find()
                    ->where('user_id', $id)
                    ->related('promoted',array('where'=>array('user_id',$this->user_id)))           
                    ->order_by('created_at', 'desc')
                    ->get();
    

    The error is as follows Fuel\Core\Database_Exception [ Error ]: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't' in 'where clause' with query: "SELECT t0.id AS t0_c0, t0.user_id AS t0_c1, t0.full AS t0_c2, t0.med AS t0_c3, t0.icon AS t0_c4, t0.map AS t0_c5, t0.place_id AS t0_c6, t0.created_at AS t0_c7, t0.updated_at AS t0_c8, t1.id AS t1_c0, t1.user_id AS t1_c1, t1.photo_id AS t1_c2, t1.created_at AS t1_c3, t1.updated_at AS t1_c4 FROM (SELECT t0.id, t0.user_id, t0.full, t0.med, t0.icon, t0.map, t0.place_id, t0.created_at, t0.updated_at FROM photos AS t0 WHERE t0.user_id = '12' ORDER BY t0.created_at DESC LIMIT 12 OFFSET 0) AS t0 LEFT JOIN promote_photos AS t1 ON (t0.id = t1.photo_id) WHERE t = 's' AND t = '2'" Obviously it shouldnt be where t='s' and t-'2' I haven't seen many examples of conditional relations maybe I'm doing something wrong. Any thoughts would be appreciated.

Howdy, Stranger!

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