CodeIgniter Forums
DataMapper 1.6.0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper 1.6.0 (/showthread.php?tid=11358)



DataMapper 1.6.0 - El Forum - 02-05-2009

[eluser]Yman[/eluser]
Hi guys,

i hav a question here

normally when we pass a query into "view", we do:
Quote:in controller/function
$data['query'] = $this->db->get('entries');

in views (i use separate php for viewing)
<html><....

<?php
foreach($query->result() as $row): ?>
<li> &lt;?=$row->watever?&gt; </li>

.../>&lt;/html&gt;

but when i try using get() method , i cant seem to get it work:
Quote:in controller/function
$query = new Horizontal();
$query->get();

in views (i use separate php for viewing)
&lt;html&gt;&lt;....

&lt;?php
foreach ($query->all as $row){

echo '<li>' . $row->watever . '</li>' .
}

it shows query not defined.

how can I do it correctly?


DataMapper 1.6.0 - El Forum - 02-05-2009

[eluser]OverZealous[/eluser]
@Yman
Are you sure you are passing the $query into your view? Is Horizontal a valid DataMapper class? Where is this error being thrown (in the controller or the view)?

You need to do some basic debugging here, because there is nothing that can be inferred from what you posted that would give you that error, unless you have a typo or other mistake in your code.


DataMapper 1.6.0 - El Forum - 02-05-2009

[eluser]Yman[/eluser]
ah , nvm i got it already



$h = Object()
$h->get()

i just need to add this :

$data['h'] = $h


DataMapper 1.6.0 - El Forum - 02-08-2009

[eluser]Daniel H[/eluser]
** Post removed - I am an idiot **


DataMapper 1.6.0 - El Forum - 02-09-2009

[eluser]macigniter[/eluser]
I am having the following issue. I get an error that my "id" field is ambiguous:

Code:
Error Number: 1052

Column 'id' in field list is ambiguous

SELECT `id` FROM (`products`) LEFT JOIN `join_clients_products` ON `products`.`id` = `join_clients_products`.`product_id` LEFT JOIN `clients` ON `clients`.`id` = `join_clients_products`.`client_id` WHERE `clients`.`id` = 9

This is my query. I can't find anything wrong with it:

Code:
$c = new Client();
$c->get_by_id($id);

$p = new Product();
$p->select('id');
$p->where_related_client('id', $id);
$p->get();

Any idea what I'm doing wrong?


DataMapper 1.6.0 - El Forum - 02-09-2009

[eluser]macigniter[/eluser]
Sorry for posting again. I'm new to the DM class and still learning... The following problem is just driving me crazy:

In my application products can be assigned to clients by checking some checkboxes and saving the record. In the scenario where I remove all product relations from an existing client and then after saving add a product again, the client somehow gets a new user id although there are existing relations to other tables (e.g. users). Any idea why DM is increasing the client id?

The update function looks like this:

Code:
$c = new Client();
$c->get_by_id($id);

$p = new Product();
$p->where_related_client('id', $id);
$p->get();

// delete old product selection for this client
$c->delete($p->all);

$c->name = $this->input->post('name');
$c->descr = $this->input->post('descr');
$products = $this->input->post('product');

if (is_array($products))
{
    $p = new Product();
    $p->where_in('id', $products);
    $p->get();
    $c->save($p->all);
}
else
{
    $c->save();
}

It looks like $c->delete($p->all) is where the problem is. I do find it strange though that even the relation from the client to the user table is being deleted... Any ideas?

UPDATE: I found the bug. Since the error only occured after adding a new product after all product/client relations have been previously deleted I had to add "if ($p->exists())" before the delete to prevent DM running a deletion on an empty relation table.

Quote:if ($p->exists()) $c->delete($p->all);



DataMapper 1.6.0 - El Forum - 02-09-2009

[eluser]OverZealous[/eluser]
[quote author="macigniter" date="1234196220"]I am having the following issue. I get an error that my "id" field is ambiguous <snip>
[/quote]

There are two solutions to your problem. The first is to modify the select to include the table name:
Code:
$p->select('products.id');

The second is to use the built-in relationships to generate your query, which will automatically add the table name:
Code:
$c = new Client();
$c->get_by_id($id);
$p = $c->product->select('id')->get();

If you don't need anything more complex than what is listed, I recommend the second method for now.

I'm working on a major update to DataMapper, and one of the changes is adding the table name to fields more often than it currently does (and only if the name hasn't been added already!).


DataMapper 1.6.0 - El Forum - 02-09-2009

[eluser]macigniter[/eluser]
Thanks Phil!

Also, would it make sense to change the behavior of "delete" when trying to delete relations although there are no relations currently stored in the relation table? Referring to my earlier post I noticed that a ->delete on a non-existing relation caused DM to delete much more than I anticipated. It'd be great if I wouldn't necessarily have to worry about the if ($p->exists()) before deleting relations.


DataMapper 1.6.0 - El Forum - 02-09-2009

[eluser]OverZealous[/eluser]
I'm not sure why that would be happening (it shouldn't be). I'll look into it later, but if you have a moment, could you send the SQL being generated by DM for that specific delete? There are several ways to get this, but I believe turning on CodeIgniter's performance profiler is the easiest, using this line of code in your controller:
Code:
$this->output->enable_profiler(TRUE);

That will help me narrow it down.


DataMapper 1.6.0 - El Forum - 02-09-2009

[eluser]macigniter[/eluser]
This is what output generates:

Code:
SELECT * FROM `clients` LIMIT 1

SELECT *
FROM (`clients`)
WHERE `id` = 1

SELECT * FROM `products` LIMIT 1

SELECT `products`.*
FROM (`products`)
LEFT JOIN `join_clients_products` ON `products`.`id` = `join_clients_products`.`product_id`
LEFT JOIN `clients` ON `clients`.`id` = `join_clients_products`.`client_id`
WHERE `clients`.`id` = 1

// why is it doing the following deletes affecting other relations with the "clients" table?
DELETE FROM `clients`
WHERE `id` = '1'

SELECT * FROM `users` LIMIT 1

DELETE FROM `join_clients_users`
WHERE `client_id` = '1'

DELETE FROM `join_clients_products`
WHERE `client_id` = '1'

SELECT * FROM `files` LIMIT 1

DELETE FROM `join_clients_files`
WHERE `client_id` = '1'

SELECT *
FROM (`products`)
WHERE `id` IN ('3', '5')  

INSERT INTO `clients` (`name`, `descr`, `created`, `updated`) VALUES ('Test Client', '', '2009-02-09 17:28:14', '2009-02-09 17:28:14')

SELECT *
FROM (`join_clients_products`)
WHERE `client_id` = 2
AND `product_id` = '3'

INSERT INTO `join_clients_products` (`client_id`, `product_id`) VALUES (2, '3')

SELECT *
FROM (`join_clients_products`)
WHERE `client_id` = 2
AND `product_id` = '5'

INSERT INTO `join_clients_products` (`client_id`, `product_id`) VALUES (2, '5')

SELECT *
FROM (`clients`)