php - Get Username from other table by author_id from another table CakePHP -


i'm using cakephp , don't know how connect data 2 tables.

here tables data

  1. users

id, username, password - , on

  1. news

id, title, text, author

now have controller, in have action gets latest news table news

$this->set('n_news', $this->news->find('all', array('limit' => 10,'order' => array('news.id desc'))));   

and have array looks this

array( (int) 0 => array(     'news' => array(         'id' => '1',         'title' => 'test',         'text' => 'test #1',         'author' => '1',         'date' => '2014-09-25 22:56:55'     ) ) 

)

and want display username of author (with id 1) instead of id. how can in secure , efficient way?

it awesome if can me. me lot in future plans creating application :)

thanks in advance!

use join relate these tables : join tables

$joins = array(                 array(                     'table' => 'users',                     'alias' => 'user',                     'type' => 'inner',                     'conditions' => array('news.auther = user.id'                 )             ); $allnews = $this->news->find('all', array('fields'=>array('news.*','user.username authername'),'joins' => $joins,'limit' => 10,'order' => array('news.id desc')));  $this->set('n_news',$allnews); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -