
Originally Posted by
me
I'm just trying to decide whether or not its worth the hassle. Considering it breaks the current implementation. I haven't found a need for it yet, that is why I ask.
I do have a example, which I avoided by using a separate table.
project_items
- id
- file (references files(id))
- thumbnail (references files(id))
files
- id
- path
In this case a project_item can have 2 files which breaks the system because dependencies are resolved to the model name. Furthermore, its always assumed that a dependency can only resolve to one field. However, in this case it can resolve to two file and thumbnail. Now, while this won't "break" the system only one field will ever be resolved to. In this case the association would never resolve to thumbnail since file preceded it.
$project_item->file (file instance => file)
Extending the File ActiveRecord class with Thumbnail seems to do the trick:
Thumbnail Class
PHP Code:
require_once('file.class.php');
class Thumbnail extends File {
public static function find() {
$args = func_get_args();
return parent::_find(__CLASS__,$args);
}
}
File Class
PHP Code:
class File extends ActiveRecord {
public static $table = 'files';
public static $primaryKey = 'id';
public static $fields = array(
'id'
,'media_type_id'
,'name'
,'path'
,'created'
);
public static $requiredFields = array(
'media_type'
,'name'
,'path'
);
public static $defaults = array(
'created'=>'NULL'
);
public static $dataTypes = array(
'id'=>'int(10) unsigned'
,'media_type_id'=>'int(10) unsigned'
,'name'=>'varchar(100)'
,'path'=>'varchar(255)'
,'created'=>'timestamp'
);
public static $transformations = array(
'created'=>array(
'save'=>'FROM_UNIXTIME({this})'
)
);
public static $foreignKeys = array(
'media_type_id'=>array('MediaType','id')
);
public static $hasMany = array('project_items');
public static $belongsTo = array('media_type');
public static function find() {
$args = func_get_args();
return parent::_find(__CLASS__,$args);
}
}
?>
Case
PHP Code:
$projects =
Project::find(
'all'
,array(
'include'=>'project_items'
)
,array(
'require'=>false
,'include'=>array('file','thumbnail')
)
,array(
'join'=>'LEFT'
,'include'=>array('media_type')
)
,array(
'join'=>'LEFT'
)
,array(
'join'=>'LEFT'
,'include'=>array('media_type')
)
,array(
'join'=>'LEFT'
)
);
echo '<pre>',print_r($projects[0]),'</pre>';
Generated SQL
Code SQL:
SELECT
t0.`id` AS t0_id
,t0.`name` AS t0_name
,t0.`summary` AS t0_summary
,t0.`weight` AS t0_weight
,t0.`created` AS t0_created
,t1.`id` AS t1_id
,t1.`project_id` AS t1_project_id
,t1.`thumbnail_id` AS t1_thumbnail_id
,t1.`file_id` AS t1_file_id
,t1.`title` AS t1_title
,t1.`summary` AS t1_summary
,t1.`weight` AS t1_weight
,t1.`created` AS t1_created
,t2.`id` AS t2_id
,t2.`media_type_id` AS t2_media_type_id
,t2.`name` AS t2_name
,t2.`path` AS t2_path
,t2.`created` AS t2_created
,t3.`id` AS t3_id
,t3.`media` AS t3_media
,t3.`mime` AS t3_mime
,t4.`id` AS t4_id
,t4.`media_type_id` AS t4_media_type_id
,t4.`name` AS t4_name
,t4.`path` AS t4_path
,t4.`created` AS t4_created
,t5.`id` AS t5_id,t5.`media` AS t5_media
,t5.`mime` AS t5_mime
FROM
projects AS t0
LEFT
JOIN
project_items AS t1
ON
t0.id = t1.project_id
LEFT
JOIN
files AS t2
ON
t1.file_id = t2.id
LEFT
JOIN
media_types AS t3
ON
t2.media_type_id = t3.id
LEFT
JOIN
files AS t4
ON
t1.thumbnail_id = t4.id
LEFT
JOIN
media_types AS t5
ON
t4.media_type_id = t5.id
Collected result Project ActiveRecord
Code:
Project Object
(
[_data:private] => ActiveRecordDataEntity Object
(
[_data:private] => Array
(
[id] => Array
(
[0] => 1
)
[name] => Array
(
[0] => Apocalyptica
)
[summary] => Array
(
[0] =>
)
[weight] => Array
(
[0] => 1
)
[created] => Array
(
[0] => 2009-04-16 11:59:11
)
[project_items] => Array
(
[0] => ActiveRecordCollection Object
(
[container:protected] => Array
(
[0] => ProjectItem Object
(
[_data:private] => ActiveRecordDataEntity Object
(
[_data:private] => Array
(
[id] => Array
(
[0] => 1
)
[project_id] => Array
(
[0] => 1
)
[thumbnail_id] => Array
(
[0] => 7
)
[file_id] => Array
(
[0] => 1
)
[title] => Array
(
[0] => apocalyptica gallery page
)
[summary] => Array
(
[0] => Website design for apocalyptica
)
[weight] => Array
(
[0] => 1
)
[created] => Array
(
[0] => 2009-04-18 09:54:54
)
[file] => Array
(
[0] => File Object
(
[_data:private] => ActiveRecordDataEntity Object
(
[_data:private] => Array
(
[id] => Array
(
[0] => 1
)
[media_type_id] => Array
(
[0] => 1
)
[name] => Array
(
[0] => apocalyptica gallery page
)
[path] => Array
(
[0] => /img/work/apocalyptica_gallery.jpg
)
[created] => Array
(
[0] => 2009-04-18 09:51:36
)
[media_type] => Array
(
[0] => MediaType Object
(
[_data:private] => ActiveRecordDataEntity Object
(
[_data:private] => Array
(
[id] => Array
(
[0] => 1
)
[media] => Array
(
[0] => image
)
[mime] => Array
(
[0] =>
)
)
)
)
)
)
)
)
)
[thumbnail] => Array
(
[0] => Thumbnail Object
(
[_data:private] => ActiveRecordDataEntity Object
(
[_data:private] => Array
(
[id] => Array
(
[0] => 7
)
[media_type_id] => Array
(
[0] => 1
)
[name] => Array
(
[0] => apocalyptica thumbnail
)
[path] => Array
(
[0] => /img/work/test.png
)
[created] => Array
(
[0] => 2009-04-20 11:01:30
)
[media_type] => Array
(
[0] => MediaType Object
(
[_data:private] => ActiveRecordDataEntity Object
(
[_data:private] => Array
(
[id] => Array
(
[0] => 1
)
[media] => Array
(
[0] => image
)
[mime] => Array
(
[0] =>
)
)
)
)
)
)
)
)
)
)
)
)
[1] => ProjectItem Object
(
[_data:private] => ActiveRecordDataEntity Object
(
[_data:private] => Array
(
[id] => Array
(
[0] => 3
)
[project_id] => Array
(
[0] => 1
)
[thumbnail_id] => Array
(
[0] => 0
)
[file_id] => Array
(
[0] => 3
)
[title] => Array
(
[0] => apocalyptica music page
)
[summary] => Array
(
[0] => apocalyptica audio page
)
[weight] => Array
(
[0] => 1
)
[created] => Array
(
[0] => 2009-04-18 12:27:29
)
[file] => Array
(
[0] => File Object
(
[_data:private] => ActiveRecordDataEntity Object
(
[_data:private] => Array
(
[id] => Array
(
[0] => 3
)
[media_type_id] => Array
(
[0] => 1
)
[name] => Array
(
[0] => apocalyptica audio page
)
[path] => Array
(
[0] => /img/work/apocalyptica_audio.jpg
)
[created] => Array
(
[0] => 2009-04-18 12:26:20
)
[media_type] => Array
(
[0] => MediaType Object
(
[_data:private] => ActiveRecordDataEntity Object
(
[_data:private] => Array
(
[id] => Array
(
[0] => 1
)
[media] => Array
(
[0] => image
)
[mime] => Array
(
[0] =>
)
)
)
)
)
)
)
)
)
)
)
)
)
)
Bookmarks