CREATE TABLE `categories` ( `id` int(10) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `slug` varchar(255) NOT NULL, `category_intro` text NOT NULL, `parent_id` int(10) NOT NULL, `created_at` int(10) NOT NULL, `updated_at` int(10) NOT NULL, `active` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
<?php
class Model_Category extends Model
{
protected static $_properties = array(
// condensed this array to only show the parent_id setup.
'parent_id' => array(
'validation' => array('required'),
'label' => 'Parent Category',
'form' => array(
'type' => 'select'
)
),
);
protected static $_observers = array( // condensed );
protected $_belongs_to = array(
'category' => array(
'key_from' => 'parent_id',
'key_to' => 'id',
'cascade_save' => false
)
);
protected $_has_many = array(
'category' => array(
'key_from' => 'id',
'key_to' => 'parent_id',
'cascade_save' => false
)
);
}
ErrorException [ Error ]: Access to undeclared static property: Model_Category::$_belongs_to PKGPATH/orm/classes/model.php @ line 341
protected static $_belongs_to = array(
'parent' => array(
'model_to' => 'category',
'key_from' => 'parent_id',
'key_to' => 'id',
'cascade_save' => false
)
);
protected static $_has_many = array(
'children' => array(
'model_to' => 'category',
'key_from' => 'id',
'key_to' => 'parent_id',
'cascade_save' => false
)
);
$listing = Model_Category::find('all', array(
'related' => array(
'parent' => array(
)
)
));
That is exactly what the belongs-to relationship does. And no relation is ever fetched unless you explicitly include it in the fetch (eager-loaded) or request it later (lazy-loaded). If you just want to get the parent you can do it like your example, or a more readable query:...but I only want the belongs_to relationship to get the parent.
Is this possible at all?
$listing = Model_Category::query()->related('parent')->get();
It looks like you're new here. If you want to get involved, click one of these buttons!