开发者

Better way to generate models from yaml in Doctrine1.2?

开发者 https://www.devze.com 2023-03-10 22:58 出处:网络
Hello Ive been working with doctrine 1.2 lately and do aliot of work from the command line.The 开发者_开发知识库problem is that when Im working on a project I change my schema alot at first.This would

Hello Ive been working with doctrine 1.2 lately and do aliot of work from the command line. The 开发者_开发知识库problem is that when Im working on a project I change my schema alot at first. This would be fine but when i run the generate-models-from-yaml, it overwrites my model classes, and alot of time I have code inside the model classes.

I should note that I use zend framework and doctrine 1.2 with the ZFDoctrine package. So i use the zf tool with the zfDoctrine Provider. The actual command I run is

zf generate-models-from-yaml doctrine  

Thanks in advance, John


One way of addressing this is to keep your custom code away from the often-rewritten classes, and place it in a different class that either extends the base model or that uses the model in question.

So if you have a BlogModel that gets rewritten all the time, you can always have a

class BlogWrapper extends BlogModel{
    function __construct(){
        parent::construct();
    }

    public function myBusinessLogic(){
        $this->functionFromBlogModel();
    }
}

or (and perhaps better - but that's just my opinion)

class MyBusinessLogic {
    private $model;

    function __construct(){
        $this->model = new BlogModel();
    }

    function doStuff($foo){
        $bar = $this->model->get($foo);
        $bar->doSomething();
        $bar->save();
    }
}

It really depends on the nature of your business logic, if the logic in question is part of the nature of the model, then perhaps the first approach is better, otherwise, if the logic simply uses or otherwise "has a" model, then the second approach is better.

Of course, this answer is completely independent from Doctrine or frameworks, it's more along the lines of "OO-philosophy" :)

0

精彩评论

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

关注公众号