Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6562759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:44:51+00:00 2026-05-25T13:44:51+00:00

I have a class Articles with a 1:N relation with table Dates. I need

  • 0

I have a class Articles with a 1:N relation with table Dates. I need to show a list of articles for each date. I use a findBySql with a Inner join.

'SELECT *
            FROM articles_art as art 
            INNER JOIN dates_dat as dat 
            ON art.id_art = dat.idart_dat 
            WHERE art.validated_art = 1 
                AND dat.date_dat <= "' . $todayDate .
            '" ORDER BY dat.date_dat, art.rank_art');

What I don’t understand is why when I try to access articles[$key]->dat, dat is an array of dates and not the object date?
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:44:52+00:00Added an answer on May 25, 2026 at 1:44 pm

    Yii has a transparent way (without writing sql) to do it. You can use scopes and relations methods on model to get data.

    The advantage to this way is that unlike findBySql that you need to define a sql each time that you want find something, you can use more than one time the scopes and relations.

    So assuming that you have a model called Article and another called Date, you models will shows like:

      class Articles extends CActiveRecord {
        …
    
        public function relations() {
           return array('date' => array(self::BELONGS_TO, 'Date', 'date_id'));
        }
    
        public function scopes() {
           return array('validated' => array('condition' => 'validated_art=1'));
        }
    
        …
      }
    
      class Date extends CActiveRecord {
        …
    
        public function relations() {
          return array('articles' => array(self::HAS_MANY, 'Article', 'date_id'));
        }
    
        public function scopes() {
          return array('byDate' => array('order' => 'date_dat'),
                       'validated' => array('condition' => 'date_dat < 2011-xx-xx'));
        }
        …
      }
    

    And your find will show likes:

      $model = Date::model()->validated()->byDate()->findAll();
      foreach($model->articles as $k => $article) {
        echo $article->title;
      }
    

    The code above is just an example to illustrate the correct way to do it on Yii, I hope that help you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of articles, and each article belongs to a section. class
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
I have class method that returns a list of employees that I can iterate
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
I have a class called Comments for articles which implements a base class of
I have a table where articles and other tables are related to a table
I have a model with articles, events and people: class Person(models.Model): id = models.IntegerField(primary_key=True)
I have a viewmodel called ArticleAdmin that includes a list of checkboxes: public class
Say I have two models, Article and Category: class Article(models.Model): category = models.ForeignKey(Category, related_name='articles')
I have two different class(class A and Class B). i want to use the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.