Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM model without properties
  • I like to create ORM model without properties, it is possible because I don't want to edit model class every time I change table columns. besides I want to use default data with MySql or Postgres.
    in Kohana, properties of ORM model is not mandatory though
  • In Fuel, it isn't either, providing the properties array can be created at runtime. Which at the moment is only possible for the MySQL/MySQLi drivers, the PDO driver lacks the support for detailed table information.

    Since this means additional queries at runtime, from a performance point of view it's not a good idea not to define the properties.

    In Fuel's ORM, the properties are required because the ORM has a lot more features then other ORM solutions, such as EAV and custom property support. Which requires the ORM to know which properties are table columns, and which not.

    Besides that, the properties array is also used by the validation observer, and by the Fieldset class to generate forms from ORM models.
  • thanks for reply,
    I agree with "properties array is used by the validation observer", and because of performance.

    like example, if I made this code first

    //controller /usr/addname/
        $usr = new Model_Usr();
        $usr->name = 'mike';
        $usr->save();

    //orm moel

        protected static $_properties = array(
            'name',
        );

    later I want to add another function

    //controller /usr/addimg/

        $usr = new Model_Usr();
        $usr->img = 'image.png';
        $usr->save();

    I need to add img column in orm model
    //orm moel

        protected static $_properties = array(
            'name',
            'img',
        );


    in this case, I need to change controller /usr/addname/
    like this?

        $usr = new Model_Usr();
        $usr->img = '';
        $usr->name = 'mike';
        $usr->save();

    Otherwise error would come? I don't want to allow NULL on Database.
    it is very difficult to maintain all of code after you edit ORM model $_properties, in my opinion.
    is there any another way to avoid such a work?

  • You can define the property to be "not null" and define a default value, which will be used by the ORM when you save the object.

    Whether or not it's difficult to maintain depends, I usually have the design fleshed out before I start, but you'll always have changes. And introducing new columns will mean controller and form changes too, adding an additional line to a model isn't such a big job.

    Regardless, the ORM model needs it properties, there is no other way.
  • all right, I could define default value on ORM model like this
    =================
            'provider' => array(
          'default' => 0,
        ),
            'pv_u_id' => array(
          'default' => '',
        ),
    ================
    thanks
  • You will also have to include 'null' => false, otherwise it will not use the default.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion