Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.6.2 (DataMapper OverZealous Edition)

[eluser]cube1893[/eluser]
Hi,

I'm experiencing performance problems with the following code, which is located in a view:

Code:
foreach($course->get()->all as $course)
{
  foreach($course->application->get()->all as $application)
  {
     $application->applicant->get();

     [..........]
     echo $application->applicant->name;
     echo $course->name;

  }
}

As there are many courses and many applications related to each course, the last query (getting the applicants) results in approx. 380 single database queries. That's very bad and the page loads very slow.
Is it possible to combine the calls of get in one query in order to have one single database query for the code snippet above?

A course has many applications, aapplication has one applicant.

I'd appreciate any help!
Daniel

[eluser]OverZealous[/eluser]
@cube1893
See Include Related.

Also, limit the fields you need to access with select statements. Don't load every column if you don't need every column.

Finally, you should be paging with over 300 items. That shouldn't be causing a slowdown, but sometimes the performance problems are in your design, not the library.

[eluser]cube1893[/eluser]
@OverZealous

Thanks, include_related helps but it only works for one-to-one relationships (applicant). How can I achieve the same for one-to-many relationships? For example if I want to access all "note" objects for an application in the second foreach loop. If I call $applicationg->notes->get() in every loop, it causes bad performance, again.

[eluser]OverZealous[/eluser]
There's no way to solve that - even for normal SQL queries. Again, you have a design problem, the performance issue is that you are trying to query so many objects at once.

There was an earlier post that had info on an extension for streaming the results so you don't have to instantiate all objects at once.

[eluser]tdktank59[/eluser]
So I am trying to drag some join fields in
Found the include_related_fields() function
Just to make sure im using it right here is how I use it

Code:
$u = new User();
$u->get_by_id($id);
$u->role->include_join_fields()->get();

However it is not bringing the fields over like it should be.
$u->role->join_start_on and $u->role->join_end_on

the fields are start_on and end_on
Every part of the relation still works just having issues dragging those values into the query.

Auto populate has_one and has_many are set to TRUE since most pages I use require most if not all of the relationships any object can have.

[eluser]tdktank59[/eluser]
[quote author="OverZealous" date="1261454449"]Thanks for the language files, Muser and tdktank59. I've added them to the distribution.

If anyone else out there would like to provide localized error messages, I would be very appreciative!

Out of curiosity, what naming structure would you prefer for the language folders:

1) Use the English names for the languages:
Code:
catalan/
english/
spanish/

2) Use the native names for the languages (in unaccented ASCII characters):
Code:
catala/
english/
espanol/

3) Use the ISO-639-1 2-letter codes:
Code:
ca/
english/ (for CI compatibility)
es/

Personally, I prefer #3 whenever possible. In fact, if I ever localize my application, I would be tempted to use an RFC 5646 structure like this:
Code:
ca/
en/
en-US/
en-GB/
es/
es-ES/
es-MX/
...
[/quote]

Doesn't really matter.
As far as CI is concered i dont think it cares besides the english folder.
However I like method 3 since It would allow more variations and what not. However would be nice if it was spelled out fully instead of shortened.

I currently am using english translations of everything since i am well english. It really all comes down to how much do you want to support. I can see Method 3 being usefull if you have multiple devs with diffrent languages being used. However method 2 can pose some problems when you get to eastern languages with the "hieroglyphics" (not sure on the term) such as Hebrew, Chinese, Japanese etc... since those are standard UTF characters.

It all comes down to user preference, and the amount of people that will have to maintain the back-end, and where they are all from.

I personally wont be working with other developers who do not know english so I can keep it in English on the back end. As long as the end user sees the translated result in their language.

[eluser]OverZealous[/eluser]
I've already decided, due to the limited feedback, to use the last format, except regional formats are <lang>_<region> (underscore not dash).

The purpose of the shortened names is simply to standardize, and the linked ISO/RFC standard is the only one I know of that takes into account regional dialects.

Anyway, the provided languages are already included in the most recent update.

[eluser]tdktank59[/eluser]
Yeah that works I guess...

So I feel really stupid on the join include issue... Turns out the values where null..
And I had typed them wrong...

[eluser]rootman[/eluser]
I'm having some trouble with the unique_pair validation. It seems not to work when saving relationships.

in my model (in validation array):
Code:
'email' => array(
            'label' => 'Email',
            'rules' => array('required', 'trim', 'unique_pair' => 'gewinnspiel_id', 'valid_email')
        ),

"gewinnspiel_id" is a has_many to has_one relationship

I save my model like that:

Code:
$gewinnspiel_antwort->save($gewinnspiel);

And it saves doublicates, same email, same relationship ... should not validate but does.

I guess it adds the relationship after saving to the db, so it does not have the id to do the unique_pair check when saving.

The workaround is to add the relationship manually:
Code:
$gewinnspiel_antwort->gewinnspiel_id = $gewinnspiel->id;
and then just save it.

But i guess it would be nice if the validation would work without the workaround ... but maybe the saving process is designed in a way which would make it very tricky to solve that due to saving relationships after saving the data.

And btw, great work, i am using dmz about three days now and it already saved my life a dozen times!

greetings

[eluser]silvergear99[/eluser]
Ok, so I have like this.
Models
Code:
class User extends DataMapper
{
    
    var $has_one = array('group','contact','user');

    var $validation = array
    (
        'username' => array
        (
            'label' => 'Username',
            
            'rules' => array('trim', 'required', 'unique', 'min_length' => 4, 'max_lenght' => 50)
        )
    );
    
    
    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
    
    function __toString()
    {
        return $this->username;
      }
}

class Contact extends Datamapper
{
    
    var $has_one = array('user');
    
    var $validation = array
    (
        'name' => array
        (
            'label' => 'Full name',
            'rules' => array('trim','required', 'min_length' => 5, 'max_length' => 255)
        )
    );
    
    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
    
    function __toString()
    {
        return $this->name;
    }
}

class Group extends Datamapper
{
    var $has_many = array('user');
    var $validation = array
    (
        'name' => array
        (
            'label' => 'Name',
            'type' => 'dropdown',
            'rules' => array('required')
        )
    );
    
    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
    
    function __toString()
    {
        return $this->name;
      }
}

view
Code:
echo $user->render_form
(
    array
    (
        'username',
        'group' => array
        (
            'label' => 'Group',
            'type' => 'dropdown'
        ),
        'contact_name' => array
        (
            'label' => 'Full name',
            'type' => 'text'
        )
    ),
    "admin/users/edit/{$user->id}",
    array
    (
        'save_button' => 'Change'
    )
);

Controller User
Code:
function edit($id = NULL)
    {
        $user = new User();
        $user->where('id',$id);
        $user->include_related('contact',array('name'));
        $user->get();
        
        if($this->input->post('username'))
        {
            $related = $user->from_array($_POST, array('username','group','contact_name'));
            $user->save($related);
        }
        
        $data['main_content'] = 'admin/users/edit_view.php';
        $data['user'] = $user;
        $this->load->view('admin/template.php', $data);
    }

Everything saves except contact_name. Im not getting any errors. Any tips ?




Theme © iAndrew 2016 - Forum software by © MyBB