Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]rzb13[/eluser]
Ok, thank you very much.

This leads me to another question related to transactions.

There's an example of using transactions in the docs, but it doesn't cover the scenario where you need to perform other save()'s in the proccess.

I have auto_transaction set to TRUE.

If I delete() relations and only then save() new ones, will both queries be in the same transaction?

This question remains for another scenario where you need to perform save()'s on different objects. Even if auto_transaction is FALSE, can I manually 'nest transactions'? Because I've seen somewhere while googling that since Datamapper uses the same Codeigniter's transaction methods, if you call trans_begin() from a second object before closing the first object's transaction, it'll then close it and start a new one, ruining the purpose.

[eluser]WanWizard[/eluser]
When you enable "auto_transaction", every database operation is encapsulated into a transaction. This obviously doesn't work when you need mutliple database operations in a single transaction.

If you need that, disable auto_transaction. You can do that for that transaction only by setting $this->auto_transaction to false on the model (and setting it back to true when you're finished).

For more information, see http://datamapper.wanwizard.eu/pages/transactions.html, or the CI user guide, as Datamapper uses the transaction methods of CI's DB class.

[eluser]rzb13[/eluser]
Hello there. Transaction works now. Thanks a lot.

Now I'm trying to get a Many-to-Many Reciprocal Self Relationship going.

User model:
Code:
'contact' => array(
            'class' => 'user',
            'other_field' => 'user',
            'reciprocal' => TRUE,
            'join_table' => 'users_contacts'),
        'user' => array(
            'other_field' => 'contact',
            'reciprocal' => TRUE,
            'join_table' => 'users_contacts'

The 'users_contacts' join_table must have some join_fields, like 'type', so I know what kind of contact is this (family, friend, workmate, etc).

But when I try to query like so:

Code:
$contacts->select('id, name, email')
                    ->where_related_user('id', $id)
                    ->include_join_fields()
                    ->order_by('name', 'ASC')
                    ->where_join_field('user', 'type', $type)
                    ->get_iterated();

I get the following error:

Quote:Error Number: 1054

Unknown column 'users_contacts.type' in 'where clause'

SELECT `users`.`id`, `users`.`name`, `users`.`email` FROM (`users`) LEFT OUTER JOIN `users_contacts` user_users_contacts ON `users`.`id` = `user_users_contacts`.`contact_id` WHERE `user_users_contacts`.`user_id` = 2 AND `users_contacts`.`type` IS NULL ORDER BY `users`.`name` ASC

As you can see, the alias is missing in users_contacts. Is it me doing it wrong or is it a bug?

[eluser]WanWizard[/eluser]
Get the latest version from https://bitbucket.org/wanwizard/datamapper, there were some fixes in this area recently.

[eluser]rzb13[/eluser]
Looks like the newest stable version is 1.8.2. That's what I already have.

Although there are 2 newer fixes called "1.8.2.1" and "tip", looking at the changes I'm afraid they don't seem to target this issue?

[eluser]Maglok[/eluser]
I have a quick query. You might remember my question about saving advanced relationships. The fix you suggested on the previous page did work, but it does not cover it 100%.

Scenario: Car->wheel->wheelcap. Imagine all advanced relationships.

Code:
//pseudocode
$car = new Car();
$wheel = new Wheel();
$wheelcap = new Wheelcap();

$wheel->save_wheelcap($wheelcap);
$car->save_wheel($wheel);

I then always get a error that is basically 'trying to save but wheel_id in wheelcap object does not have a default value'. Normally datamapper would save the parent and then pass the parent id to the child to use, but it seems it doesn't do that in this scenario.

What am I missing?

[eluser]WanWizard[/eluser]
[quote author="rzb13" date="1362491782"]Looks like the newest stable version is 1.8.2. That's what I already have.

Although there are 2 newer fixes called "1.8.2.1" and "tip", looking at the changes I'm afraid they don't seem to target this issue?[/quote]
Allways use tip, it contains the latest bugfixes.

I don't have your exact situation here, but I tried to recreate it using this query:
Code:
$o = $u->where_related_orders('id', 1)
    ->include_join_fields()
    ->where_join_field('orders', 'status', 1)
    ->get_iterated();

Which produces
Code:
SELECT 'users`.*
FROM ('users`)
LEFT OUTER JOIN `orders` orders_orders ON `users`.`id` = `orders_orders`.`user_id`
WHERE `orders_orders`.`item_id` =  1
AND `orders_orders`.`status` =  1

This uses the alias just fine, hence my remark that you should try the latest version.

[eluser]WanWizard[/eluser]
[quote author="Maglok" date="1362664708"]I then always get a error that is basically 'trying to save but wheel_id in wheelcap object does not have a default value'. Normally datamapper would save the parent and then pass the parent id to the child to use, but it seems it doesn't do that in this scenario.

What am I missing?[/quote]
I looked in the code, and yes, save() always saves $this first, before trying to relate any objects to it. And if those objects are aren't saved, it will save them first too (which the latest fix).

Do you have an exact error message and line, because this doesn't tell me much about the issue you're having...

[eluser]Maglok[/eluser]
I happen to know you read dutch. Smile This is the specific error it throws. The customproject is the wheel in the example, while the crm_project_custom_education is the wheelcap.

The two have a one to one relationship and customproject_id is a column in crm_project_custom_education. The column does not have a default value, because we always require it to be filled. I can work around the error by making NULL the default, but that feels dirty.

Code:
<div id="container">
  <h1>Er is een database fout opgetreden.</h1>
  <p>Error Number: 1364</p><p>Field 'customproject_id' doesn't have a default value</p><p>INSERT INTO `crm_project_custom_education` (`max_students`, `execution`, `location`, `materials`, `room`, `catering`, `created_on`) VALUES ('1', '', '', '', '', '', '2013-03-08 08:52:19')</p><p>Filename: [root]\system\database\DB_driver.php</p><p>Line Number: 330</p> </div>

[eluser]WanWizard[/eluser]
I think the problem is that in the earlier fix, I added a global check if the related object was new, and if so, save it first. Which is causing this error, because the required foreign key is missing in case the related object contains the foreign key.

I've pushed a fix to bitbucket.




Theme © iAndrew 2016 - Forum software by © MyBB