1. Had to add *`id`* to the Attributes *$_accessible* field array (as
described by heavyKevy in a post above). If making foreign table IDs
available for a joint table is necessary, why isn't the bake process doing
that when it analyzes the foreign key structure?
2. Manually setting join data dirty after the entity patch:
*$listing->dirty('attributes._joinData',
true);* - The documentation's examples for doing this are when you
manually set entity data *outside* an entity-related call. *patchEntity*
implies there will be dirty data, so it should handle it internally.
Besides, when it comes to editing & i go to patch the existing entity with
the POST data, maybe the _joinData is unaltered, and therefore manually
setting it dirty is *incorrect*. i can't make assumptions with the POST
data on what's dirty or not. That, to me, is exactly what *patchEntity*
is for. However, i haven't even managed to get the joint record to save
without *all* the modifications described in Kevin's previous post.
3. In the *ListingsAttributesTable* validation, it doesn't seem correct
that *allowEmpty* for the two foreign IDs should be required to save the
data, but it doesn't seem to work otherwise. In fact, if either of those
IDs is empty for the joint table, validation should *fail*, but these
validations will permit it.
And none of that explains why, after following the documentation examples &
Kevin's gracious effort to help me out, why the
*`listings_attributes.value`* column still doesn't receive the
*_joinData.value* POST data...
On Tuesday, 3 March 2015 03:11:03 UTC-5, José Lorenzo wrote:
>
> What did you have to do that was not in the documentation? It would be
> helpful to add it to the docs so other don't get stuck as well in the same
> problem
>
> On Tuesday, March 3, 2015 at 7:25:01 AM UTC+1, Joe T. wrote:
>>
>> i'm REALLY sorry for the delay getting back to you. And unfortunately,
>> i'm still not there. It's CLOSE though:
>>
>> i did exactly as you suggested in your last post. Set up my Entities &
>> Tables just as you described. Set up the controller to manually set the
>> _joinData "dirty" and called the save.
>>
>> i get the ListingsAttributes record now! But no value from the text input
>> (_joinData.value).
>>
>> The POST data is correct:
>>
>> Array
>> (
>> [attributes] => Array
>> (
>> [3] => Array
>> (
>> [id] => 1
>> [_joinData] => Array
>> (
>> [value] => Off-street
>> )
>> )
>> )
>> )
>>
>> That's exactly what i've been getting since i change to use _joinData...
>>
>> Here's the Entity after merging & setting _joinData dirty:
>>
>> Array
>> (
>> [attributes] => Array
>> (
>> [0] => Array
>> (
>> [id] => 1
>> )
>> )
>> )
>>
>> As before, the "value" input got lost. However, there is FINALLY an
>> Attributes array in the Listing entity with the correct Attributes.id. The
>> result is, i get a ListingsAttributes record with the Listings.id and
>> Attributes.id, but nothing in the *value* column.
>>
>> This is making me crazy & i'm on the edge of scrapping this thing &
>> starting over with something else. i never expected this much difficulty
>> getting some basic data relationships to work after following documentation
>> exactly (and then having to make *undocumented* changes that still only
>> get me part-way there. i don't think i'm doing anything radical or
>> unconventional with my form, so i don't understand the difficulty of making
>> the ORM function as advertised.
>>
>> i'm frustrated, obviously. It's taken me this long to come back to follow
>> up because it becomes harder & harder to come back & try again. i'm almost
>> there, i just don't know why the *value* input won't go into the Entity
>> data with everything else.
>>
>> If you're moving on, i totally understand. But if you'd like to look at
>> the code directly, let me know. i'll have to put a version online without
>> sensitive info in it...
>>
>> Thanks for at least getting me this far. :)
>> -joe
>>
>>
>>
>>
>> On Wednesday, 25 February 2015 04:57:35 UTC-5, heavyKevy wrote:
>>>
>>> Joe,
>>>
>>> Here is what I did:
>>> In the attribute entity:
>>> protected $_accessible = [
>>> 'id' => true,
>>> 'name' => true,
>>> 'listings' => true,
>>> ];
>>>
>>> In the Listings_Attributes Table:
>>> public function validationDefault(Validator $validator)
>>> {
>>> $validator
>>> ->add('id', 'valid', ['rule' => 'numeric'])
>>> ->allowEmpty('id', 'create')
>>> ->add('listing_id', 'valid', ['rule' => 'numeric'])
>>> ->allowEmpty('listing_id', 'create')
>>> ->notEmpty('listing_id')
>>> ->add('attribute_id', 'valid', ['rule' => 'numeric'])
>>> ->allowEmpty('attribute_id', 'create')
>>> ->notEmpty('attribute_id')
>>> ->allowEmpty('value');
>>>
>>> return $validator;
>>> }
>>>
>>>
>>> In the Listings Controller: Add method:
>>> public function add()
>>> {
>>> $listing = $this->Listings->newEntity();
>>> if ($this->request->is('post')) {
>>> $listing = $this->Listings->patchEntity($listing,
>>> $this->request->data,['associated'=>['attributes._joinData']]);
>>> $listing->dirty('attributes._joinData',true);
>>> if ($this->Listings->save($listing)) {
>>> $this->Flash->success('The listing has been saved.');
>>> return $this->redirect(['action' => 'index']);
>>> } else {
>>> $this->Flash->error('The listing could not be saved.
>>> Please, try again.');
>>> }
>>> }
>>> $attributes = $this->Listings->Attributes->find('list', ['limit'
>>> => 200]);
>>> $this->set(compact('listing', 'attributes'));
>>> $this->set('_serialize', ['listing']);
>>> }
>>>
>>> In the Add template of the Listing: add.ctp
>>> <?= $this->Form->create($listing); ?>
>>> <fieldset>
>>> <legend><?= __('Add Listing') ?></legend>
>>> <?php
>>> echo $this->Form->input('name');
>>> //echo $this->Form->input('attributes._ids', ['options' =>
>>> $attributes]);
>>>
>>> if(!empty($attributes)){
>>> $i =0;
>>> foreach( $attributes as $k => $attr){
>>> echo
>>> $this->Form->input('attributes.'.$i.'.id',['type'=>'checkbox','value'=>$k,'label'=>$attr]);
>>>
>>>
>>> echo
>>> $this->Form->input('attributes.'.$i.'.name',['type'=>'hidden','value'=>$attr]);
>>> echo
>>> $this->Form->input('attributes.'.$i.'._joinData.value');
>>> $i++;
>>> }
>>> }
>>> //echo $this->Form->select('attributes._ids',
>>> $attributes,['label'=>'Attributes','multiple' => 'checkbox']);
>>>
>>>
>>> ?>
>>> </fieldset>
>>> <?= $this->Form->button(__('Submit')) ?>
>>> <?= $this->Form->end() ?>
>>>
>>>
>>> I don't believe I changed anything else.. Just used what was baked, and
>>> it works for me.
>>>
>>> --Kevin
>>>
>>>
--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.