开发者

Data form multiple models in one module in Symfony

开发者 https://www.devze.com 2023-01-28 04:53 出处:网络
I\'m learning Symfony and I came across one issue and I couldn\'t find an answer: I\'m taking as an example the Jobe开发者_开发百科et tutorial from Jobeet-1.4-Doctrine-en.

I'm learning Symfony and I came across one issue and I couldn't find an answer: I'm taking as an example the Jobe开发者_开发百科et tutorial from Jobeet-1.4-Doctrine-en.

On day 3 [ day 3 ]: It's all about the model and this where I am stuck When generating the frontend, the following command is run:

$ php symfony doctrine:generate-module --with-show --non-verbose-templates frontend job JobeetJob

It generates the module 'job' in accordance with the provided module 'JobeetJob'

My question now is: I have an another module called 'JobeetArticles' where people can learn how to write better CV and such things. I want to have both data from 'JobeetJob' and 'JobeetArticles' available for the 'job' module. How can i accomplish that? I hope my question is clear

Regards


Generally if your relationships are setup correctly (I'm not sure if you NEED to do anything in Symfony to allow this) you would just do something like in the jobeet tutorials with categories...

in the actions.class.php file for job you have executeIndex and inside of it you have $this->categories = JobeetCategoryPeer::getWithJobs();

and what this is doing is finding the model JobeetCategoryPeer and calling the getWithJobs() function in that model and then it returns that data back to the job controller to send to the view as $categories.

So you would want to do something similar, so in JobeetArticlesPeer create a function to return the data you want...

static public function getArticles()
{
  $criteria = new Criteria();
  //whatever criteria you want in here

  return self::doSelect($criteria);  

}

And in your JobeetJob actions.class.php file something like

public function executeIndex(sfWebRequest $request)
{
  $this->articles = JobeetArticlesPeer::getArticles();
}

And lastly in your indexSuccess.php for JobeetJob

<?php foreach ($articles as $article): ?>
  <div class="article">
    <div class="article_title">
      <h1>
        <?php echo link_to($article, 'article', $article) ?>
      </h1>
      <?php echo $article->getAuthor() //field name for author ?>
    </div>
    <div class="article_content">
      <?php echo $article->getContent() //field name for article content ?>
    </div>
  </div>
<?php endforeach; ?>

And that should give you a general idea of how to access data from other models

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号