php - CakePHP: findById() with Containable not returning expected associations -
here's model:
class question extends appmodel { public $hasmany = array( 'casequestions' => array('classname'=>'question', 'foreignkey'=>'parent_id') ); public $hasone = array( 'casestudy' => array('classname'=>'question', 'foreignkey'=>'parent_id') );
here's action in controller:
public function admin_delete_case_question($question_id) { $this->question->behaviors->load('containable'); $this->question->contain( array('casestudy')); $question = $this->question->findbyid($question_id ); debug($question); exit;
the debug above returns this:
array( 'question' => array( 'id' => '78', 'nickname' => '', 'content' => 'sdgsdfgs', 'type' => 'cq', 'option1' => 'sdfgsdfg', 'option2' => '', 'option3' => '', 'option4' => '', 'time' => '-1', 'difficulty' => '0.0000', 'slope' => '0.0000', 'chance' => '0', 'experiment' => false, 'created' => '2013-05-02 16:30:29', 'modified' => '2013-05-02 16:30:29', 'status' => null, 'perm_id' => '76', 'notes' => null, 'is_deleted' => false, 'answer_id' => '0', 'parent_id' => '77', 'order' => null ), 'casestudy' => array( 'id' => null, 'nickname' => null, 'content' => null, 'type' => null, 'option1' => null, 'option2' => null, 'option3' => null, 'option4' => null, 'time' => null, 'difficulty' => null, 'slope' => null, 'chance' => null, 'experiment' => null, 'created' => null, 'modified' => null, 'status' => null, 'perm_id' => null, 'notes' => null, 'is_deleted' => null, 'answer_id' => null, 'parent_id' => null, 'order' => null ) )
i dont' understand why casestudy array null because when @ record (id 77) in db data correct. doing wrong?
in example... question hasone casestudy; means question parent.
based on data, question has parent_id, i'm assuming mean casestudy parent , question child. looks association backwards (normally parent wouldn't have parent_id).
replace hasone association, belongsto instead:
public $belongsto = array( 'casestudy' => array('classname'=>'question', 'foreignkey'=>'parent_id') );
that ensure casestudy parent, , question child.
with way it's set up, cakephp trying casestudy has parent_id equal question.id; since don't have data in db cakephp finds nothing , returns null values. in reality, casestudy parent , question has parent_id equal casestudy.id
Comments
Post a Comment