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

mysql sharding, Horizontal sharding table

because one table have many records.

i spilt the table as month (date).

one month one table.

such as table named notes.

tables is:

notes notes_201303 notes_201304

in model class i defined the model class like this:


class Notes extends Model {
    public function initialize () {.....}
    public function getSource() {
        return 'pre_notes';
    }
}

now,i can't use the model class in project . because the model method getSource defined the db table.

how can define the model db table dynamic? or have other method resloved the question ? except use the Phalcon\Db

Hi,

I realized that the getSource method is called everytime before a query is issued. If you could determine the required table by time of the call you could just return the propper tablename.

Take a look here: https://blog.phalcon.io/post/44715359754/phalcon-1-0-0-beta-released in the Vertical/Horizontal Sharding Improvements section). In the selectReadConnection method you might set a flag or name for the source and return it in the getSource method. I think this might work if the table meta data does not change. Just an idea, dont know if it works or has side effects.



17.8k

michaelkrone , thank you

in my old thread https://forum.phalcon.io/discussion/177/in-model-selectreadconnection-s-param-is-fixed-or-custom-

i asked a question about selectreadconnection . but this method return a connection, not a table.

now i decide don't split a table to many table, i used the mysql partitioning to resolved this question

@netstu, u can use $this->setSource()

class Notes extends Model {
    public function initialize () {
       //check date
       $this->setSource('notes_201303');
    }
}


17.8k

@Marcio Paivam, i can't understand your mean.

what different in model's class use setSource in initialize or use getSource?

they should be the same

Hello.

Marcio suggests nearly the same procedure as I did: If you know (or can somehow compute) wich date is requested inside the initialize method (or any other method Phalcon calls automaticly before fetching the data, selectReadConnection etc.) you can set the source to use. You do not need a getSource implementation then.