We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Same table structure spilt by YYYYMM.

I have 2 tables (1-n relationship, "Keyword" has many "KeywordData") and they are spilt by YYYYMM. They are in the same database.

Example, Jan 2014 records are in keyword_201401 & keyword_data_201401. Feb 2014 records in keyword_201402 & keyword_data_201402

How do I define the table name in the models so that the values are write/read from the correct _YYYYMM tables?

keyword_201401 (keyword_id, keyword, date) keyword_data_201401 (data_id, keyword_id, source)

keyword_201402 (keyword_id, keyword, date) keyword_data_201402 (data_id, keyword_id, source)



98.9k

Try:

class Robots extends Phalcon\Mvc\Model
{
    public function getSource()
    {
        return "robots_" . date("Ym");
    }
}
edited Mar '14

The date("Ym") uses the current YearMonth.

How do I specify the YearMonth that I want to read & write?

<?php

class Robots extends \Phalcon\Mvc\Model
{
 public function getSource()
  {
    return "robots_" . $myYearMonth
  }
    public function initialize()
    {
        $this->hasMany("id", "RobotsParts_" . $myYearMonth, "robots_id");
    }
}
<?php

class RobotsParts extends \Phalcon\Mvc\Model
{
public function getSource()
  {
    return "robotsparts_" . $myYearMonth
  }
    public function initialize()
    {
        $this->belongsTo("robots_id", "Robots_" . $myYearMonth, "id");
    }

}

Thank you

I think you might have to create a factory function or class to set the items like belongsTo you would normally set in an initialize():

class Robots extends \Phalcon\Mvc\Model
{

    /* Other functions. Have a blank initialize() */

    public static function factory($yearMonth)
    {
        $robot = new self();
        $robot->setSource('robots_' . $yearMonth);
        $robot->hasMany("id", "RobotsParts_" . $yearMonth, "robots_id");
        /* Any other "$this->" statements you would normally have in your initialize() function */

        return $robot;
    }
}

Call it in your code like:

$robots = Robots::factory('201401');

This should return a Robots object with the correct info. This code is off the top of my head and is in no way tested, so let us know if it works!