How to retrieving data in objects not array in cakephp

Cakephp

Posted on 06 Aug 15 by Vickey
  601



Cakephp by defualt return data as an array.For example if I want fetch a user's record from database i will use the below function and it will return
data in array form

public function index() {
        
        $data=$this->User->findById(4);
        pr($data); die;
        
}
Array
(
    [User] => Array
        (
            [id] => 4
            [name] => Kamryn Smitham
            [email] => ischmidt@hotmail.com
            [password] => qui
            [remember_token] => 
            [created_at] => 2015-05-08 09:22:18
            [updated_at] => 2015-05-08 09:22:18
        )

)

but in some cases we need data in objects so for get data in objects form we use 'Set::map()' function

public function index() {
        
        $data=$this->User->findById(4);
        $data = Set::map($data);
        pr($data); die;
        
}
stdClass Object
(
    [_name_] => User
    [id] => 4
    [name] => Kamryn Smitham
    [email] => ischmidt@hotmail.com
    [password] => qui
    [remember_token] => 
    [created_at] => 2015-05-08 09:22:18
    [updated_at] => 2015-05-08 09:22:18
)

here we get data as objects


If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com