spadefoot / kohana-orm-leap

An ORM module for the Kohana PHP framework that is designed to work with all major databases.
http://spadefoot.github.io/kohana-orm-leap/
100 stars 25 forks source link

Model: set multiple (expected) fields #53

Closed taai closed 12 years ago

taai commented 12 years ago

I want to do the same what I did in Kohana_ORM - set all values at once and filter the expected data:

<?php
$user = ORM::factory('user');

// array of data, array of expected keys
$user->values($_POST, array('first_name', 'last_name'));

As I see, with Leap you can use load():

<?php
$user = new Model_Leap_User;
$user->load($_POST);

But it will use that data to set all possible fields, aliases and adaptors (wich probably is ok), and it will not exclude the primary key(s).

I guess that I have to filter that data myself:

<?php
// get the list of columns/fields
$expected = array_flip(Model_Leap_User::columns());

// ignore the primary key(s)
foreach (Model_Leap_User::primary_key() AS $key)
{
    unset($expected[$key]);
}

$expected = array_flip($expected);

// extract only expected data
$post = Arr::extract($_POST, $expected);

$user->load($post);

...and I have to do it everywhere.

Leap should do at least the same what Kohana_ORM can do.

Possible solutions:

Add a second (optional) argument to the load() function:

public function load(Array $columns = array(), Array $expected = NULL)

Or create a function values() or with different name set_fields() wich set's the data, excluding primary key(s) and second (optional) argument should be the array of expected values.

Possibly there should be a method or variable where to store the list of expected fields, so I don't have to always touch the Controllers, but it would be stored in Model. But then it probably may vary on each action - creating/updating/searching/deleting, so I don't know abou that.

I hope that you understood what I mean.