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

[eluser]naren_nag[/eluser]
UPDATE:

So here's how I fixed this bug.

I have a method in a controller where I start building a query. Then I pass the object to a helper where I continue to build the query (chaining various things together). In the helper I also create another object to get all the groups the current user belongs to. And that's where things fell apart.

At least when you're in the helper, it looks like you can't pass an object and create a new object there. Now, I can't figure out why this should be, so I have to be missing something here.

If anybody would like to try and pick this apart with me, I'm game. For the moment I have fixed this by getting the list of groups in the controller and passing it to the helper function.

cheers,

naren

[eluser]tdktank59[/eluser]
Just finished reading up on the new stuff and now updating a few queries...

heres one that has stumped me thus far. (Even stumped me on the last version too)

Code:
$sql = "SELECT  `problems`.`id`,
                `members`.`sid`, `members`.`name`,
                `problem_statuses`.`name`,
                `owner`.`first_name`, `owner`.`last_name`,
                `assigned_to`.`first_name`, `assigned_to`.`last_name`
            FROM `problems`
            LEFT JOIN `members` ON `members`.`id` = `problems`.`member_id`
            LEFT JOIN `problem_statuses` ON `problem_statuses`.`id` = `problems`.`status_id`
            LEFT JOIN `users` as owner ON `users`.`id` = `problems`.`owner_id`
            LEFT JOIN `users` as assigned_to ON `users`.`id` = `problems`.`assigned_to`
            WHERE `problems`.`id` IN ".$problem_id."
                AND (`members`.`name` LIKE '%".$sSearch."%'
                    OR `members`.`sid` LIKE '%".$sSearch."%'
                    OR `problem_statuses`.`name` LIKE '%".$sSearch."%')
            ORDER_BY ".$order." LIMIT ".$iDisplayStart.", ".$iDisplayLength;

Heres what I was using (thanks OverZealous for the help on this one too!)
Code:
$problem = new Problem();

$problem->join_related('member', array('name'));
$problem->join_related('status', array('name'));
$problem->join_related('owner', array('first_name', 'last_name'));
$problem->join_related('assigned_to', array('first_name', 'last_name'));

// create special query section
$search = '';
$sSearch = $problem->db->escape_str($sSearch);
foreach(array('`members`.`name`', '`members`.`sid`', '`status_problem_statuses`.`name`') as $col)
{
    if(!empty($search)) { $search .= ' OR '; }
    $search .= "{$col} LIKE '%{$sSearch}%'";
}

// note: calling directly on the db library, to bypass DataMapper.
$problem->db->where('(' . $search . ')');
$problem->where_in('id',$problem_id);

if ($source == 'inbox')
  $problem->where('assigned_to_id',$user_id);
else
  $problem->where('owner_id',$user_id);

if (isset($order)) $problem->db->order_by($order);
$problem->limit($iDisplayLength,$iDisplayStart);
$problem->get();

The select statement is not that important but those are the values I am using...

And the above DMZ code is throwing this error From the looks of it, its not doing any of the joins...

Quote:A Database Error Occurred

Error Number: 1054

Unknown column 'members.name' in 'where clause'

SELECT `problems`.*, `assigned_to_users`.`first_name` AS assigned_to_first_name, `assigned_to_users`.`last_name` AS assigned_to_last_name FROM (`problems`) LEFT OUTER JOIN `users` as assigned_to_users ON `assigned_to_users`.`id` = `problems`.`assigned_to_id` WHERE (`members`.`name` LIKE '%%' OR `members`.`sid` LIKE '%%' OR `status_problem_statuses`.`name` LIKE '%%') AND `problems`.`id` IN ( [LIST OF IDS REMOVED] ) AND `problems`.`assigned_to_id` = '2' ORDER BY `problems`.`id` asc LIMIT 10

Ill continue trying to get this to work... However Im pretty stumped in how to do this...

[eluser]OverZealous[/eluser]
@naren_nag
My guess is that you have two classes with the same name somewhere. When you create the new object, it isn't a DataMapper object, but whatever the alternate is (ie: a controller, library, or something else that has the same name as the DM model).

@tdktank59
I can't see what exactly is wrong, but my guess is that the query is getting cleared somehow. It usually happens when you look up an object while building a query, which can be tricky, like this:
Code:
// Example of what NOT to do
$object->include_related('other', 'name');
$widget = new Widget();
$widget->get_by_id($widget_id);
$object->where_related($widget);
$object->get();

The call to $widget->get_by_id will have reset the query. Instead, always load all of your objects at once, and build the query after, like this:
Code:
// Example of what to do
$widget = new Widget();
$widget->get_by_id($widget_id);
$object->include_related('other', 'name');
$object->where_related($widget);
$object->get();

Also, I think you should be able to safely change this:
Code:
// create special query section
$search = '';
$sSearch = $problem->db->escape_str($sSearch);
foreach(array('`members`.`name`', '`members`.`sid`', '`status_problem_statuses`.`name`') as $col)
{
    if(!empty($search)) { $search .= ' OR '; }
    $search .= "{$col} LIKE '%{$sSearch}%'";
}

// note: calling directly on the db library, to bypass DataMapper.
$problem->db->where('(' . $search . ')');

to this:
Code:
$problem->group_start()
    ->like('name', $sSearch)
    ->like('name', $sSearch)
    ->like_related('status', 'name', $sSearch)
->group_end();

(Don't forget to replace all the join_related method calls with include_related. I plan on removing join_related completely when I eventually move to DMZ 2.x (but that could be years. ;-) )

[eluser]bEz[/eluser]
@phil

Using 1.4.0 (I know about 1.4.1)

I know you're going to fine me for not providing enough info, but trust me, I'm checking the naming conventions and relationships where possible.
Anyhow, I have used FIREPHP to log the fact that I am successfully instantiating a model object, view the data retrieved (get), as well as able to assign new data to the object.
However, when I perform a save(), I am getting the following error:
Code:
Fatal error: Class name must be a valid object or a string in C:\home\furious4\ci\rr\application\libraries\datamapper.php on line 1553

I am pulling my hair out, as I've tried doing this with use of the new extensions, and even now back to bare bones assignment.

-bEz

[eluser]bEz[/eluser]
[quote author="bEz" date="1249435679"]@phil

Using 1.4.0 (I know about 1.4.1)

I know you're going to fine me for not providing enough info, but trust me, I'm checking the naming conventions and relationships where possible.
Anyhow, I have used FIREPHP to log the fact that I am successfully instantiating a model object, view the data retrieved (get), as well as able to assign new data to the object.
However, when I perform $obj->save(), I am getting the following error:
Code:
Fatal error: Class name must be a valid object or a string in C:\home\furious4\ci\rr\application\libraries\datamapper.php on line 1553

I am pulling my hair out, as I've tried doing this with use of the new extensions, and even now back to bare bones assignment.

-bEz[/quote]

Just to let you know, my fix to this issue was to do the following:
Code:
$obj->skip_validation()->save();

[eluser]OverZealous[/eluser]
@bEz
The only reason to see that error is that you have a typo in the class name of one of your relationships. Just looking at the DMZ source code sees that that line occurs here:
Code:
// In count()
1549  $related_properties = $this->_get_related_properties($related_field);
1550  $class = $related_properties['class'];

1553  $object = new $class();

The reasons your workaround works is you aren't validating the input, which means you are not validating a relationship rule (such as required), and therefore DMZ isn't counting the related records.

The error will be found on the child object, so that if you are doing this:
Code:
$object->save($child);
and the Object model has a required relationship to Child, Child will have the typo in the class for Object.

[eluser]bEz[/eluser]
Ok, let me check the validation definitions so that I can close this trouble ticket Smile

[eluser]OverZealous[/eluser]
* The error would be in the $has_one or $has_many arrays.

[eluser]tdktank59[/eluser]
Allright got it working!

However here is the dmz code
Code:
$problem = new Problem();

$problem->member->get();
$problem->status->get();
$problem->owner->get();
$problem->assigned_to->get();

$problem->group_start()
            ->like_related('member', 'name', $sSearch)
            ->or_like_related('member', 'sid', $sSearch)
            ->or_like_related('status', 'name', $sSearch)
        ->group_end();


$problem->where_in('id',$problem_id);

if ($source == 'inbox')
  $problem->where('assigned_to_id',$user_id);
else
  $problem->where('owner_id',$user_id);

if (isset($order)) $problem->db->order_by($order);
$problem->limit($iDisplayLength,$iDisplayStart);
$problem->get();

The include_related does not work for some reason... (might want to look into that?)
and for some reason the sql this generates is this:
Quote:SELECT `problems`.*
FROM (`problems`)
LEFT OUTER JOIN `members` as members ON `members`.`id` = `problems`.`member_id`
LEFT OUTER JOIN `problem_statuses` as status_problem_statuses ON `status_problem_statuses`.`id` = `problems`.`status_id`
WHERE (
`members`.`name` LIKE '%%'
OR `members`.`sid` LIKE '%%'
OR `status_problem_statuses`.`name` LIKE '%%'
)
AND `problems`.`id` IN ( LIST OF IDs REMOVED )
AND `problems`.`assigned_to_id` = '2'
ORDER BY `problems`.`id` asc
LIMIT 10

It works but should it also be joining the users table twice??? (owner, assigned_to)

[eluser]OverZealous[/eluser]
@tdktank59
The join_related function calls include_related. It no longer does anything. There is no reason it would work differently. I do not see where you are joining a user table in your example code.




Theme © iAndrew 2016 - Forum software by © MyBB