Sunday, February 5, 2012

Re: Using Join Tables in Cakephp - result in view.ctp

WHERE 1=1 is by default.
You will see that everywhere if you do not provide any 'conditions'

You can change that by adding 'condition' to your query list.

for example:

$subscriberName = $this->Subscriber->find('list', array('conditions'
=> array('id' => $mySubscriber), 'fields' => array('name')));

then cake will put out something like SELECT name FROM Subscribers WHERE id=6

If you do this,
$organizationStatusActive = $this->OrganizationStatus->find('list',
array('conditions' => array('OrganizationStatus.name' => 'Active'),
'fields' => array('OrganizationStatus.id')));

you will get something like

SELECT OrganizationStatus.id FROM Organizations WHERE
OrganizationStatus.name = 'Active'


Sorry if you find any errors from this reply~ had to write real
quick... and I am not a cake expert either~


On Sun, Feb 5, 2012 at 10:20 PM, Team Kemster
<kundeservicekemster@gmail.com> wrote:
> Well I am joining a script with the table name "port_group_entity", so it is
> not possible to change that table.
>
> When I make an error in the table name, the error message shows a totally
> correct join query (except the table name of course). The only thing that is
> bothering me is the end "WHERE 1=1". Why is that coming?
>
> /TheMuller
>
> Den 05/02/2012 kl. 23.13 skrev Justin Edwards <justinledwards@gmail.com>:
>
> There is a typo here. Do you have a table named port_groups_entity,
> sport_groups_entities?  The errors should be followable.
>    array(
>            'table' => 'port_groups_entity',
>
> You also seem to be  using some odd names on your tables.  I try to always
> avoid using two plurals unless that table is a join table (habtm) for two
> other tables.   If I do has association through tables, I name that table
> logically for the relationship that it has.   Such as students, classrooms,
> has a join table enrollments that has extra data based the many to many.
>
> You should debug($var) on all of your variables to see what is actually
> there.
>
>
> On Sun, Feb 5, 2012 at 1:41 PM, Team Kemster <kundeservicekemster@gmail.com>
> wrote:
>>
>> Hi Stephen,
>>
>> Thank you for your answer.
>>
>> Well I would say you are right, if it wasn't for that I am able to
>> use both the tables for it's self. But joining them gives the problems.
>>
>> I also tried to use some examples on binding the tables with a model for
>> both tables, but no luck.
>>
>> Yes, the table has "name" for a column (tables shown in first mail).
>>
>> Is the pull out in the ctp files correct?
>>
>> /TheMuller
>>
>> Den 05/02/2012 kl. 18.54 skrev Stephen Speakman
>> <stephen@ninjacodermonkey.co.uk>:
>>
>> > Hi
>> >
>> > I may not know the answer to your question but shouldn't your model be
>> > named:
>> >
>> > sport_group.php and SportGroup extends AppModel ?
>> >
>> > port_groups_entity table should be named "port_group_entities"  ----
>> > PortGroupEntities
>> >
>> > I would personally correct all the filenames, class names and
>> > $this->Model lines etc to represent proper CakePHP conventions, then I would
>> > use pr($this->SportGroup->findByUrl($url)); etc to view the results
>> > retrieved from the query...
>> >
>> > If name isn't in there maybe it's an error with your MySQL table? Maybe
>> > check recursive? (Apologies if I missed any info, my thunderbird client
>> > isn't threading mail list topics =/)
>> >
>> > Kind Regards
>> > Stephen
>> >
>> >
>> > On 05/02/2012 17:18, TheMuller wrote:
>> >> Hi,
>> >>
>> >> I have looked, search and founded, but not the answer to my questions:
>> >> How do I get the result of my join table in my view.ctp? Am I doing
>> >> something wrong?
>> >>
>> >> I have followed some guides from bakery and other blogs, and created a
>> >> setup like shown below. But why does the error in view.ctp say:
>> >> "Notice (8): Undefined index:  name"
>> >>
>> >> Code:
>> >>
>> >> models/sportsgroup.php
>> >> <?php
>> >> class SportsGroup extends Appmodel {
>> >>     var $name = 'SportsGroup';
>> >> }
>> >> ?>
>> >>
>> >> controllers/sports_groups_controllers.php
>> >> <?php
>> >>
>> >> class SportsGroupsController extends AppController {
>> >>     var $helpers = array ('Html', 'Form', 'Js' =>  array('Jquery'),
>> >> 'Javascript', 'GoogleMapView');
>> >>     var $name = 'SportsGroups';
>> >>
>> >>     function index() {
>> >>       $this->SportsGroup->recursive = 0;
>> >>       $this->set('sportsgroups', $this->SportsGroup->find('all',
>> >> array('joins' =>  array(
>> >>         array(
>> >>             'table' =>  'port_groups_entity',
>> >>             'alias' =>  'GroupsEntity',
>> >>             'type' =>  'inner',
>> >>             'conditions'=>  array('SportsGroup.group_id =
>> >> GroupsEntity.guid')
>> >>             )
>> >>         )
>> >>         )));
>> >>       $this->set('title_for_layout', 'Sportsclubs online');
>> >>
>> >>     }
>> >>     function view($url = null) {
>> >>     if (!$url) {
>> >>         $this->Session->setFlash('Invalid id for Post.');
>> >>         $this->redirect('/sportsclub/');
>> >>     }
>> >>     $this->set('post', $this->SportsGroup->findByUrl($url));
>> >>     }
>> >> }
>> >>
>> >> ?>
>> >>
>> >> view/sportsgroups/index.ctp (or view.ctp)
>> >> <?php foreach ($sportsgroups as $post): ?>
>> >> <?php echo $post['SportsGroup']['title']?>  (it catches this one)
>> >> <?php echo $post['SportsGroup']['name']?>  (but not this one)
>> >> <?php endforeach; ?>
>> >>
>> >> view/sportsgroups/view.ctp
>> >> <?php echo $post['SportsGroup']['title']?>  (it catches this one)
>> >> <?php echo $post['SportsGroup']['name']?>  (but not this one)
>> >>
>> >> The database table are like:
>> >> sports_groups: id | group_id | title
>> >> groups_entity: guid | name
>> >>
>> >> Please help me, I used 2-3 days on this one..
>> >>
>> >> /TheMuller
>> >>
>> >
>> > --
>> > 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
>> > cake-php+unsubscribe@googlegroups.com For more options, visit this group
>> > at http://groups.google.com/group/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
>> cake-php+unsubscribe@googlegroups.com For more options, visit this group
>> at http://groups.google.com/group/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
> cake-php+unsubscribe@googlegroups.com For more options, visit this group at
> http://groups.google.com/group/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
> cake-php+unsubscribe@googlegroups.com For more options, visit this group at
> http://groups.google.com/group/cake-php

--
Richard Joo

--
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
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: