Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Cannot update using ORM
  • Hey i'm trying to update my table with the following command but its not working. why? $is_valid = Model_Blog_User::find()->where(array('user_login' => $user_login, 'user_activation_key' => $user_activation_key));
    if($is_valid->count() == 1){;
    $is_valid->user_status = '1';
    $is_valid->update();
    } Its a link that is provided to a user after signing up to activate their account. The primary key is user_id. user_login is the username and user_activation_key is the activation key
  • That doesn't get anything, that just returns the query object. You have to do either get() or get_one() to execute the query.
  • Solved! Used id instead! user $post->user_id; to get the last id and used it instead!
  • Harro Verton wrote on Friday 10th of February 2012:
    That doesn't get anything, that just returns the query object. You have to do either get() or get_one() to execute the query.

    Suppose $data = $post->get(); Then how do i read the datas?
  • You can only get a query. The way find() works is explained in the documentation. In short, 'first', 'last' and 'all' do an implicit get(), find() without parameters creates a query object that you can use to chain additional methods to. A query object has to be executed using get() or get_one() to get a result.
  • Harro Verton wrote on Friday 10th of February 2012:
    You can only get a query. The way find() works is explained in the documentation. In short, 'first', 'last' and 'all' do an implicit get(), find() without parameters creates a query object that you can use to chain additional methods to. A query object has to be executed using get() or get_one() to get a result.

    I got the data in $data like $post->get(). but how can i read the values in $data?
  • get() returns an array of objects. Did you actually look at the documentation? This is all explained including examples.
  • Harro Verton wrote on Friday 10th of February 2012:
    get() returns an array of objects. Did you actually look at the documentation? This is all explained including examples.

    I got the data into $user_data by $user_data = $post->get(); but how do i read the objects in the array?
    when using find:('1') it returns an object so i can do $post->user_email and read the data but doing $user_data doesn't work. how do i do it? Sorry, i'm really new to all these stuffs. and its really good to see the real developers of fuelphp helping us out.
    thanks!
  • If $post is an ORM model object, you can't get() it! You can only get() find queries. So:
    // find all users (returns an array of Model_Blog_User objects)
    $users = Model_Blog_User::find('all');
    
    // find the first user (returns a Model_Blog_User object)
    $user = Model_Blog_User::find('first');
    
    // find all users called 'John'  (returns an array of Model_Blog_User objects)
    $users = Model_Blog_User::find()->where('first__name', '=', 'John')->get();
    
    // find the first user called  'John'  (returns a Model_Blog_User object)
    $user = Model_Blog_User::find()->where('first__name', '=', 'John')->get_one();
    

    The queries that return a single model object will return NULL if nothing is found. The queries that return multiple objects return an empty array if nothing is found.
  • Harro Verton wrote on Friday 10th of February 2012:
    If $post is an ORM model object, you can't get() it! You can only get() find queries. So:
    // find all users (returns an array of Model_Blog_User objects)
    $users = Model_Blog_User::find('all');
    
    // find the first user (returns a Model_Blog_User object)
    $user = Model_Blog_User::find('first');
    
    // find all users called 'John'  (returns an array of Model_Blog_User objects)
    $users = Model_Blog_User::find()->where('first__name', '=', 'John')->get();
    
    // find the first user called  'John'  (returns a Model_Blog_User object)
    $user = Model_Blog_User::find()->where('first__name', '=', 'John')->get_one();
    

    The queries that return a single model object will return NULL if nothing is found. The queries that return multiple objects return an empty array if nothing is found.

    I'm not sure if you got what i mean. I did -
    $post = Model_Blog_User::find()->where('user_login', Input::post('user_login'));
    $user_data = $post->get();
    
    It executes, returns the array into $user_data. but how do i read the objects inside the array?? When i get some values like
    $post = Model_Blog_User::find($user_id);
    
    , I can read the fields like
    $post->user_email
    
    . But doing
    $user_data->user_email
    
    doesn't work cause its a not object. So how do i get the values from
    $user_data
    
    ?? print_r($user_data) returns the following but how to read the values?
    Array ( [13] => Model_Blog_User Object ( [_is_new:Orm\Model:private] => [_frozen:Orm\Model:private] => 
    [_data:Orm\Model:private] => Array ( [user_id] => 13 [user_login] => KickB [user_pass] => 
    5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8 [user_name] => kick bottowski [user_email] => 
    kick@awesome.com [user_url] => [user_activation_key] => 36bb6dcb8bf4e07fe50a76be29cc31b8 
    [user_status] => 1 [user_last_login] => 1328898667 [created_at] => 1328898667 [updated_at] => 1328898691 ) 
    [_original:Orm\Model:private] => Array ( [user_id] => 13 [user_login] => KickB [user_pass] => 
    5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8 [user_name] => kick bottowski [user_email] => 
    kick@awesome.com [user_url] => [user_activation_key] => 36bb6dcb8bf4e07fe50a76be29cc31b8 
    [user_status] => 1 [user_last_login] => 1328898667 [created_at] => 1328898667 [updated_at] => 1328898691 ) 
    [_data_relations:Orm\Model:private] => Array ( ) [_original_relations:Orm\Model:private] => Array ( ) 
    [_view:Orm\Model:private] => [_iterable:protected] => Array ( ) ) ) 
    
    
  • As I wrote, get() returns an array of objects. The array has an index value equal as the primary key. How do you access array values? That has nothing to do with ORM, that's PHP 101. You can use foreach to iterate over it. You can use $arrayname to access an array element, ...
  • Harro Verton wrote on Saturday 11th of February 2012:
    As I wrote, get() returns an array of objects. The array has an index value equal as the primary key. How do you access array values? That has nothing to do with ORM, that's PHP 101. You can use foreach to iterate over it. You can use $arrayname to access an array element, ...

    I'm so sorry I feel totally stupid right now. Just expected you to do everything without me trying anything! This wont happen again. Thanks for all your help.

Howdy, Stranger!

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

In this Discussion