Here is the code for the models and the table.
 
agent model:
 
var $hasMany = array(
  'Bookmark' => array(
   'className' => 'Bookmark',
   'foreignKey' => 'agent_id',
   'dependent' => false,
   'conditions' => '',
   'fields' => '',
   'order' => '',
   'limit' => '',
   'offset' => '',
   'exclusive' => '',
   'finderQuery' => '',
   'counterQuery' => ''
  ),
 
investor model:
var $hasMany = array(
  'Post' => array(
   'className' => 'Job',
   'foreignKey' => investor_id',
   'dependent' => false,
   'conditions' => '',
   'fields' => '',
   'order' => '',
   'limit' => '',
   'offset' => '',
   'exclusive' => '',
   'finderQuery' => '',
   'counterQuery' => ''
  ),
 
bookmark model
 
var $belongsTo = array(
  'Agent' => array(
   'className' => 'Agent',
   'foreignKey' => 'agent_id',
   'conditions' => '',
   'fields' => '',
   'order' => ''
  ),
  'Post' => array(
   'className' => 'Post',
   'foreignKey' => 'post_id',
   'conditions' => '',
   'fields' => '',
   'order' => ''
  )
 );
 
 
USERS TABLE
id
username
password
group_id (either investor or agent)
email
....
 
AGENTS TABLE
id
user_id
fname
lname
email
.....
 
INVESTORS TABLE
id
user_id
fname
lname
email
.....
 
POSTS TABLE
id
investor_id
title
description
created
mofified
.....
 
BOOKMARKS TABLE
id
agent_id
post_id

  _____  

From: RyOnLife [mailto:[email protected]] 
Sent: April-03-09 1:55 PM
To: [email protected]
Subject: Re: Data from multiple Models


Can you show us what the agents, investors and users tables look like? Also,
can you give the code you've used to create the associations for all of the
models?



On Fri, Apr 3, 2009 at 11:13 AM, Dave Maharaj :: WidePixels.com (via Nabble)
<ml-user%2b169603-205417...@...
<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2581666&i=0> >
wrote:


I have included code as requested:

I am trying to get info from 2 models (Posts and Bookmarks) displayed on a
third controller(Agents profile page). 
The user hasMany Bookmarks belongsTo Post hasMnay Bookmarks

I have agents_controller to display the agents/profile

Bookmarks Table:
id
agent_id
post_id

Posts Table
Id
Investor_id
Title
Description
...... Other fields

The Posts are not related to the Agents, only to the Investors. The agent
Bookmarks the Post so in the console baked view it shows related bookmarks
and just the ID post_id and agent_id....whch is useless since I need the
title so the agent has some clue on what the bookmark will take him too.

user has the option to bookmark a post
the bookmark saves the agent_id and the post_id so in the profile it shows
Related Bookmarks straight from baked but all it shows is the ID , AGENT_ID
and POST_ID but I want the title of the post and the description so when an
agent views his profile he understands what the bookmarks are naturally....

What I would like is the Bookmark to get the info from the post also so it
shows the Post.title and Post.description

the view has the standard "foreach ($agent['Bookmark'] as $bookmark): but
what I cannot figure out is how do I get the Post info?


View file:
<?php __('Related Bookmarks');?>
        </h3>
        <?php if (!empty($agent['Bookmark'])):?>
        <?php
                $i = 0;
                foreach ($agent['Bookmark'] as $bookmark):
                        $class = null;
                        if ($i++ % 2 == 0) {
                                $class = ' class="altrow"';
                        }
                ?>
        <?php echo $bookmark['id'];?>
                <?php echo $bookmark['agent_id'];?>

I WOULD LIKE TO REMOVE THE agent_id and ID here and have the Post.Title and
Post.description here 


                <?php echo $bookmark['job_id'];?>
         <?php echo $html->link(__('View', true), array('controller'=>
'posts', 'action'=>'view', $bookmark['post_id'])); ?>
                 <?php echo $html->link(__('Edit', true),
array('controller'=> 'bookmarks', 'action'=>'edit', $bookmark['id'])); ?>
                 <?php echo $html->link(__('Delete', true),
array('controller'=> 'bookmarks', 'action'=>'delete', $bookmark['id']),
null, sprintf(__('Are you sure you want to delete # %s?', true),
$bookmark['id'])); ?>
        <?php endforeach; ?>
        <?php endif; ?>

Agents_controller :

function profile($slug = null)
      {
          $this->Agent->recursive = 1;
                  $user = $this->User->findBySlug($slug);
          $id = $user['Agent']['id'];
          if (!$id) {
              $this->Session->setFlash(__('Invalid Agent.', true));
              $this->redirect(array('action' => 'index'));
          }
          $this->set('agent', $this->Agent->read(null, $id));
                  //pr($user);
      }




-----Original Message-----
From: RyOnLife [ryan.mckil...@...
<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2581269&i=0> ]
Sent: April-03-09 1:03 AM
To: cake-...@...
<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=2581269&i=1> 
Subject: Re: Data from multiple Models



Dave, having a little trouble following your post, but this is how you use
the containable behavior:

$this->User->contain = array('Post.title', 'Post.description',
'Post.Bookmark'); $user = $this->User->findBySlug($slug);

If you're having trouble figuring out which models to contain, try:

$this->User->recursive = 2;
$user = $this->User->findBySlug($slug);
pr($user);

Then take a look at the output and you'll have a better idea of what to
contain.

-Ryan



I am trying to get info from 2 models displayed on a third controller. I
think contain Post.title and Post.description but have no idea how to add
that to the controller.

The user hasMany Bookmarks belongsTo Post hasMnay Bookmarks

I have users_controller to display the user/profile

user has the option to bookmark a post
the bookmark saves the user_id and the post_id so in the profile it shows
Related Bookmarks straight from baked but all it shows is the ID , USER_ID
and POST_ID naturally....

What i would like is the Bookmark to get the info from the post also so it
shows the Post.title and Post.description

the view has the standard "foreach ($user['Bookmark'] as $bookmark): but
what i cannot figure out is how do i get the Post info?

The profle function is stright from baked also:

function profile($slug = null)
      {
          $user = $this->User->findBySlug($slug);
          if (!$slug) {
              $this->Session->setFlash(__('Invalid User.', true));
              $this->redirect(array('action' => 'index'));
          }
          $this->set('user', $this->User->read(null, $slug));
      }

Thanks for any insight.

Dave





--
View this message in context:
http://n2.nabble.com/Data-from-multiple-Models-tp2578660p2578784.html
Sent from the CakePHP mailing list archive at Nabble.com.






  _____  

View this message in context: Re:
<http://n2.nabble.com/Data-from-multiple-Models-tp2578660p2581666.html> Data
from multiple Models
Sent from the CakePHP mailing list archive
<http://n2.nabble.com/CakePHP-f19694.html>  at Nabble.com.




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

Reply via email to