Hi,
First of all I forgot to mention that I'm using CakePhP 2.0, PHP
5.3.1, and MySQL5.1.41, and Apache 2.2.14.
Some investigation has been done.
So the validation inhereted from the parent model is working. The only
thing I had to do is to no to put the name of the parent model on the
view as I mentioned.
so just simply use:
<?php echo $this->Form->create('ModelB');?>
<fieldset>
<legend><?php echo __('Backadmin Add ModelB'); ?></legend>
<?php
echo $this->Form->input('field1FromA'); // no need to use
ModelA.field1FromA
echo $this->Form->input('field2FromA');
// etc...
echo $this->Form->input('field1FromB');
echo $this->Form->input('field2FromB');
// etc...
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
This would be pretty cool, but some problem still exist. The form is
based on ModelB, and Form->input(...) function tries to figure out
which type the given field is. This works perfectly for the fields of
the given model but not its parent. It will not see the underlying
table of ModelA so eg. TINYINT(1) is not discovered well and simple
text input is showed instead of a checkbox. But, if I specify the
model in the view the checkbox appears well. (but the validation goes
away...).
I check the dump of the $object retrieved in _introspectModel() in
FormHelper.php which has type eg. ModelB. The validation part of the
dumped array seems ok, contains both rules from the child model and
from the parent model. The problem is that the useTable and table part
contain only the table used by ModelB, models_bs. If I take a closer
look at that object dump (printed by debug($object);) I found
parentClass, and parent. If it does not find the attribute in the
model and in the models joind to it (via habtm), it could look for
them in the model's parent. So this part should be corrected:
if ($key === 'fields') {
if (!isset($this->fieldset[$model]['fields'])) {
$fields =
$this->fieldset[$model]['fields'] = $object->schema();
}
if (empty($field)) { // it's not my case now
foreach ($object->hasAndBelongsToMany
as $alias => $assocData) {
$this->fieldset[$object->alias]['fields'][$alias] = array('type'
=> 'multiple');
}
return
$this->fieldset[$model]['fields'];
} elseif
(isset($this->fieldset[$model]['fields'][$field])) { //
returns here in success
return
$this->fieldset[$model]['fields'][$field];
} else {
// it returns here if the give field is not found, inhereted fields
should be handled from here
return
isset($object->hasAndBelongsToMany[$field]) ? array('type'
=> 'multiple') : null;
}
}
Hope this will help to somebody.
On okt. 18, 21:58, Gergő Nagy <[email protected]> wrote:
> Hi,
>
> I did not find any material about inheritance, and form embedding on cake
> php sites so far.
> I tried to use
> this:http://bakery.cakephp.org/articles/santino83/2011/02/19/behavior_for_...
> but did not work perfectly.
> Basicly I use MTI (multiple table inheritance)
> I have classes ModelA extends AppModel, class ModelB extends ModelA, class
> ModelC extends ModelA and class ModelD extends AppModel.
> Let's say ModelA belongsTo ModelD and ModelD hasMany ModelA of course.
>
> Tables:
> model_as(id, model_d_id, ...)
> model_bs(id, ...)
> model_cs(id, ...)
> model_ds(id, ...)
>
> Id fields in model_bs and model_cs are the same as in model_as. So before we
> can save a ModelB entity we have to save ModelA to have common id. This is
> handled more or less by that behavior mentioned above. (One bug exists
> related to this.)
>
> Is there any built in support from cake php, to support this kind of
> architecture?
> What I would need from the child entites point of view are:
> - embedding the fields of the parent model on the child's form, now I put
> them there by hand. Like ModelA.field1, ModelA.field2, etc. while form is
> created eg. with $this->Form->create('ModelB')
> - validation defined in ModelA, disapperad in the view of the child entity
> - types of the fields of the parent class are not ok, on the parent form
> they are ok, but on the a child form they aren't. Eg. booleans became simple
> textfields.
> - fields that are come via relationship of ModelA belongsTo property from
> ModelD, has to be set by hand in the controller of ModelB
> - on the update form these kind of fields (mentioned in the previous line),
> are not field well, selected element has to be set by hand
>
> I used the $uses = ('ModelA', 'ModelB') in the controller of ModelB.
>
> So is there anybody who faces these situation or is it planned to be
> supported in cake php?
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php