Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Find by id
  • hi, I've got Projects with donations: within the Project model action_view I try to get all the donation for one project. I've got that for now but it gives me a cardinality violation
    $view->set_global('donations',
                     Model_Donation::find(array(
                                'select' => array('id', 'amount', 'project_id', 'user_id'),
                                 'where' => array('project_id', '=', 1),
                                 ))) ;
    

    All i want is to retrieve the donations by project_id (column) thanks
  • I'm not sure where you got the idea that would work, I hope none of the examples in docs looks like this. The find() method requires a first param that is either a primary-key value (the ID) or 'first', 'last' or 'all'.
  • well I did follow the docs, modifying a bit. Now something is strange, I changed the code a bit, now I get the SQL error:
    Fuel\Core\Database_Exception [ Error ]: SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s) with query: "SELECT `t0`.
    `id` AS `t0_c0`, `t0`.`amount` AS `t0_c1`, `t0`.`project_id` AS `t0_c2`, `t0`.`user_id` AS `t0_c3`, `t0`.`created_at` AS `t0_c4`, `t0`.`updated_at` AS 
    `t0_c5` FROM `donations` AS `t0` WHERE `t0`.`id` = ('id', 'amount', 'project_id') LIMIT 1
    

    Is it normal that my parameters used for SELECT are used for WHERE ? Now if I only leave 1 value, I get nothing back, thou my page displays all other infos. new coe:
     $view->set_global('donations',
                      Model_Donation::find(array(
                                      'select' => array('id', 'amount', 'project_id'),
                                      'where' => array('project_id', '=', $id),
                      ))) ;
    
  • Could you please at least read what I write? Otherwise I have the feeling that I'm talking to a wall which tends to lead nowhere.
    $view->set_global('donations',
                     Model_Donation::find('first', array(
                                'select' => array('id', 'amount', 'project_id', 'user_id'),
                                 'where' => array('project_id', '=', 1),
                                 ))) ;
    
  • ok ok i read. i know have :
    Model_Donation::find($id, array('related' => array('project')))
    

    but that returns an object, not an array. Any idea why?
  • If 'first', 'last' or a specific ID (which seems the case here) will always return 1 object. Use 'all' if you want multiple objects. But judging by your previous examples the project_id isn't your primary key, thus you won't want to use it as such and will still have to pass it as a where array. Please checkout the docs as all of this is explained there.
  • Sorry to insist but the doc says find will return an array of instances http://docs.fuelphp.com/classes/model_crud/methods.html#/method_find And I use : Model_User::find('all') and that returns an array
  • That is Model_Crud, that has nothing to do with the ORM.
  • Thanks mate, I worked it out. Sorry if I seemed like I didn't pay attention. I got confused with array within array within array.

Howdy, Stranger!

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

In this Discussion