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 keyword201401 & keyworddata201401. Feb 2014 records in keyword201402 & keyworddata201402

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

keyword201401 (keywordid, keyword, date) keyworddata201401 (dataid, keywordid, source)

keyword201402 (keywordid, keyword, date) keyworddata201402 (dataid, keywordid, source)



83.5k

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!