Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Remove EAV properties from DB?
  • Hi, could you tell me please, how to remove already exists EAV properties?


    class Model_Model extends \Orm\Model
    {
    protected static $_has_many = array('properties');
    protected static $_eav = array(
    'properties' => array(
    'attribute' => 'name',
    'value' => 'value')
    );
    }
    $model = Model_Model::find(1);
    $model->new_properties = 'value';
    $model->old_properties = NULL; // don't delete from DB
    unset($model->old_properties); // not working
  • Harro VertonHarro Verton
    Accepted Answer
    Unset is the way to do it. But with all changes to the model, you need to save the model to save it's changes, and that includes EAV data.
  • Sorry for necroposting, but the same problem again.

    I need to delete all eav properties from DB rather then break relations:


    // Here is break relation (project_id = null), 
    // but not physically delete
    $project = Model_Project::find(1);
    unset($project->settings);
    $project->save();
    // Here is delete settings from DB, 
    // but re-created it after $project->save()
    $project = Model_Project::find(1);
    foreach($project->settings as $s)
    {
    $s->delete();
    }
    $project->save();
    // Hooray, everything as expected
    $project = Model_Project::find(1);
    foreach($project->settings as $s)
    {
    $s->delete();
    }
    unset($project->settings);
    $project->save();
    So, my question is: "Is it normal behavior for Has Many relations"? 

    Here is my models:

    class Model_Project extends \Orm\Model
    {
    protected static $_has_many = array(
    'settings' => array(
    'key_from' => 'id',
    'model_to' => 'Model_Settings',
    'key_to' => 'project_id',
    'cascade_save' => true,
    'cascade_delete' => true,
    ),
    );

    protected static $_eav = array(
    'settings' => array(
    'attribute' => 'key',
    'value' => 'value',
    )
    );
    }

    class Model_Settings extends \Orm\Model
    {
    protected static $_belongs_to = array(
    'project' => array(
    'key_from' => 'project_id',
    'model_to' => 'Model_Project',
    'key_to' => 'id',
    'cascade_save' => true,
    'cascade_delete' => false,
    ),
    );
    }

  • It is logical the first attempt doesn't work. That deletes setting records in the table, but doesn't delete the related objects from the project, so when you save the project, the ORM sees the still related setting objects as new, and saves them.

    It's probably quicker to do:

    // disconnect the settings from the project
    $project = Model_Project::find(1);
    unset($project->settings);
    $project->save();

    // delete the settings for this project
    Model_Settings::query()->where('project_id', '=', $project->id)->delete();

Howdy, Stranger!

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

In this Discussion