Thursday, February 25, 2010

Re: how to adress specific data from related models?

Hello ... me - again :)

ok if I do a debug on $this->data

I get the array you supposed, showing me bookingpositions containing a
field lodging_id
but how do i extract data from the db table lodgings for the row with
the matching id?

May I use a pure sql syntax in a query to get the value?
Seems the field() syntax wont work for me or just with me (sorry again
I am a newbie..)

CU

Michael

On 24 Feb., 11:25, Jeremy Burns <jeremybu...@me.com> wrote:
> The problem you are experiencing now is that you are trying to get the value $this->data['Lodging']['id'], which doesn't exist.
>
> I suggest you look at your data array (debug($this->data);). You'll most likely find your value in:
>
> $this->data['BookingPosition']['Lodging']['id']
>
> or
>
> $this->data['BookingPosition']['lodging_id']
>
> Seriously - debug out your $this->data array and just follow the path through it to the value you want to reach.
>
> You might, of course, find that your array doesn't contain the value you are looking for, in which case you need to go back to the controller/form that is creating your $this->data array and amend it, or find your value another way.
>
> Jeremy Burns
> jeremybu...@me.com
>
> On 24 Feb 2010, at 09:59, mivogt-LU wrote:
>
> > Hi Jeremy,
>
> > your suggested solution also results with an error because of a wrong
> > Index.
> > Notice (8): Undefined index: Lodging [APP\models\booking_position.php,
> > line 78]
>
> > if I do
> > $numberOfBeds = $this->Lodging-
> >> field('Bettenanzahl',array('Lodging.id'=>$this->data['Lodging']
> > ['id']));
>
> > I have
> > Model bookingpositions:
> > var $belongsTo = array(
> >            'Booking' => array(
> >                    'className' => 'Booking',
> >                    'foreignKey' => 'booking_id',
> >                    'conditions' => '',
> >                    'fields' => '',
> >                    'order' => ''
> >            ),
> >            'Bookingstatus' => array(
> >                    'className' => 'Bookingstatus',
> >                    'foreignKey' => 'bookingstatus_id',
> >                    'conditions' => '',
> >                    'fields' => '',
> >                    'order' => ''
> >            ),
> >            'Lodging' => array(
> >                    'className' => 'Lodging',
> >                    'foreignKey' => 'lodging_id',
> >                    'conditions' => '',
> >                    'fields' => '',
> >                    'order' => ''
> >            ),
>
> > and model lodgings:
> > class Lodging extends AppModel {
> >    var $name = 'Lodging';
> >    var $displayField = 'Zimmernummer';
> >    //The Associations below have been created with all possible keys,
> > those that are not needed can be removed
>
> >    var $belongsTo = array(
> >            'Building' => array(
> >                    'className' => 'Building',
> >                    'foreignKey' => 'building_id',
> >                    'conditions' => '',
> >                    'fields' => '',
> >                    'order' => ''
> >            ),
> >            'Level' => array(
> >                    'className' => 'Level',
> >                    'foreignKey' => 'level_id',
> >                    'conditions' => '',
> >                    'fields' => '',
> >                    'order' => ''
> >            )
> >    );
>
> >    var $hasMany = array(
> >            'BookingPosition' => array(
> >                    'className' => 'BookingPosition',
> >                    'foreignKey' => 'lodging_id',
> >                    'dependent' => false,
> >                    'conditions' => '',
> >                    'fields' => '',
> >                    'order' => '',
> >                    'limit' => '',
> >                    'offset' => '',
> >                    'exclusive' => '',
> >                    'finderQuery' => '',
> >                    'counterQuery' => ''
> >            )
> >    );
>
> > so both are linked with each other ...
>
> > Suggests? TIA
>
> > Michael
>
> > On 24 Feb., 08:32, Jeremy Burns <jeremybu...@me.com> wrote:
> >> If you are doing this from within the BookingPosition model, '$this' points to the BookingPosition model. So by trying to access $this->BookingPosition you are in fact trying to access $this->BookingPosition->BookingPosition, if that makes sense! Instead, try $this->Lodging->field...etc. This should work, so long as you have a relationship set up between BookingPosition and Lodging in the models. If you are accessing this from a controller, then you need to use $this->BookingPosition->Lodging...etc
>
> >> Jeremy Burns
> >> jeremybu...@me.com
>
> >> On 24 Feb 2010, at 07:26, mivogt-LU wrote:
>
> >>> Hello Paul,
>
> >>> sadly there seems to be an error in adressing the model the way you
> >>> told me:
>
> >>> Notice (8): Undefined property: BookingPosition::$BookingPosition [APP
> >>> \models\booking_position.php, line 78]
> >>> Notice (8): Trying to get property of non-object [APP\models
> >>> \booking_position.php, line 78]
> >>> Fatal error: Call to a member function field() on a non-object in D:
> >>> \x173\xampp\htdocs\myca\app4\myapp\models\booking_position.php on line
> >>> 78
>
> >>> with line 78 as you suggested
>
> >>>> $numberOfBeds = $this->BookingPosition->Lodging->field('numberOfBeds', array('Lodging.id'=>$this->data['Lodging']['id']));
>
> >>> I can access i.e. number of kids form the model like this:
> >>> $K = $this->data['BookingPosition']['AnzahlKinder'];
>
> >>> so $this->BookingPosition->... do not loook that wrong to me...
>
> >>> Any idea why it does not work and how to work around?
>
> >>> TIA
>
> >>> Michael
>
> >>> On 23 Feb., 10:18, WebbedIT <p...@webbedit.co.uk> wrote:
> >>>> If I understand your model and field names correctly, after submitting
> >>>> your BookingPosition form which includes the field Lodging.id, you
> >>>> should be able to use:
>
> >>>> $numberOfBeds = $this->BookingPosition->Lodging->field('numberOfBeds',
> >>>> array('Lodging.id'=>$this->data['Lodging']['id']));
>
> >>>> For more info on the Model->field() method see:
>
> >>>>http://book.cakephp.org/view/453/field
>
> >>>> HTH
>
> >>>> Paul.
>
> >>> Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others with their CakePHP related questions.
>
> >>> You received this message because you are subscribed to the Google Groups "CakePHP" group.
> >>> To post to this group, send email to cake-php@googlegroups.com
> >>> To unsubscribe from this group, send email to
> >>> cake-php+unsubscribe@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en
>
> > Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with their CakePHP related questions.
>
> > You received this message because you are subscribed to the Google Groups "CakePHP" group.
> > To post to this group, send email to cake-php@googlegroups.com
> > To unsubscribe from this group, send email to
> > cake-php+unsubscribe@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: