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

[eluser]Maglok[/eluser]
Since datamapper models are effectively extending a library, you need to get the instance of the CI object I believe.

Code:
$CI =& get_instance();

Then reference CI calls with $CI-> like $CI->load->view('test');
#92

[eluser]toadies[/eluser]
[quote author="WanWizard" date="1330523791"]
In this setup, the Order table contains the FK 'customer_id'.

When deleting the customer record, Datamapper will fetch the relation definition from the Customer model, and it will check if 'cascading_delete' is set to TRUE on the relation (which it is by default).
[/quote]

My two tables is customers and equipments has one-to-one relations.
In MySQL I have a FK in the equipment table with customer_id as the FK to id in customers table. On Delete is set to Cascade in equipments.

cascade_delete is set to true.

Quote:If so, it will check if there is a relationship table. If there is, it will delete the records from that table, but will not delete any Order records.

If there is no relationship table, it will check if Customer or Order contains the FK that defines the relation.

I have no relationship table.

Quote:If the FK is in the other table, and the other table is equal to the current objects table (only true in a self-referencing relation) and the FK is in the current table, the FK will be set to NULL, and nothing will happen to the other table.

In all other cases, the related record will be deleted.

After this process, the current record itself will be deleted. If you're using a DB capable of transactions, this entire process is wrapped in a transaction to ensure integrity.

If that doesn't happen, then you have just found a bug.

When I try to delete here is my warning message from my script.
Code:
A Database Error Occurred

Error Number: 1452

Cannot add or update a child row: a foreign key constraint fails (`pools_dev`.`equipments`, CONSTRAINT `PK_Customers` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE CASCADE)

UPDATE `equipments` SET `customer_id` = NULL WHERE `customer_id` = 5

The script I don't think is even trying to delete the equipment relationship. Only try to update it to Null.

Is this a bug or am I doing something wrong?

**Edit: I resolved by setting cascade_delete to FALSE and letting mysql handling the relationship.
#93

[eluser]WanWizard[/eluser]
If you use constraints and ON CASCADE DELETE, you should set 'cascade_delete' to FALSE as they don't work together.

This is documented in the manual.

If it tries to set it to NULL instead of deleting it, that would be a bug. I'll have a look.
#94

[eluser]Luis Pacheco[/eluser]
[quote author="Maglok" date="1330590027"]Since datamapper models are effectively extending a library, you need to get the instance of the CI object I believe.

Code:
$CI =& get_instance();

Then reference CI calls with $CI-> like $CI->load->view('test');[/quote]
Thank you very much!
#95

[eluser]Maglok[/eluser]
Been finding out a lot about datamapper, the more I use it the more I like it.

Here is one I can't figure out, and am not sure if I understand the docs right.

Lets assume that a Person has Hobbies.

So Person has_many Hobby and Hobby has_many Person.

In the join table between the two, is the amount of years the Person is doing that Hobby.

I know how to get all the hobbies the person has, like so:

Code:
$hobbies->include_join_fields()->where_related('person', 'id', $person)->get();

My 'problem' is the following: If I want to print a list of all hobbies in the system and how many years a person is doing them (so including hobbies the person does not practice) how would I go about it?

I would need the join field 'years_active', but I do not want to limit the query to just the hobbies that have a relation. Does datamapper have any way to do that? Or should I start pasting result sets together?

Greets!
#96

[eluser]tarciozemel[/eluser]
Using MATCH...AGAINST in ->where()

Hi, folks!

I 1.8.1 version, I was using

Code:
...
$companies->where('MATCH (foo) AGAINST ("bar" IN BOOLEAN MODE)');
...

and everything was running fine.

But, when I upgraded to 1.8.2, now I get an error. When I check the query, seems like DM is put quots in MATCH word.

Code:
... AND `MATCH` (foo) AGAINST ("bar" IN BOOLEAN MODE) ...

Any ideas about how avoid this?

Regards!
#97

[eluser]Ikkes[/eluser]
Hello,

I ran into some error that i can't seem to solve. Not even with Google :-)

What I want is the following:
I have 2 databases. 1 database has user data and the other has the website data. I want to authenticate with the user database. Below you can find my database.php config file and my auth.php model file.

database.php
Code:
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'database_user';
$db['default']['password'] = '*****';
$db['default']['database'] = 'website_database';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

$db['users']['hostname'] = 'localhost';
$db['users']['username'] = 'database_user';
$db['users']['password'] = '*****';
$db['users']['database'] = 'user_database';
$db['users']['dbdriver'] = 'mysql';
$db['users']['dbprefix'] = '';
$db['users']['pconnect'] = TRUE;
$db['users']['db_debug'] = TRUE;
$db['users']['cache_on'] = FALSE;
$db['users']['cachedir'] = '';
$db['users']['char_set'] = 'utf8';
$db['users']['dbcollat'] = 'utf8_general_ci';
$db['users']['swap_pre'] = '';
$db['users']['autoinit'] = TRUE;
$db['users']['stricton'] = FALSE;

auth.php
Code:
class Auth extends DataMapper {

var $table = 'members';
var $db_params = 'users';
}

The problem I have is that I get the following database error:
Code:
Error Number: 1146

Table 'website_database.members' doesn't exist

DESCRIBE `members`

Filename: /bladiebladiebla/libraries/datamapper.php

Line Number: 645

It seems that CodeIgniter/DataMapper uses the default settings but in the model I've overwritten the db params with the users database.

My question is: What am I doing wrong?

With kind regards,
Dennis
#98

[eluser]North2Alaska[/eluser]
I'm just starting to use DM. I've created a simple model to query a table, but I want to add a prefix. My tables have multiple prefixes and I will have to define the prefix for each table/class.

class User extends DataMapper {

var $prefix = "auth_";

My problem is the prefix is not honored.

A Database Error Occurred
Error Number: 1146
Table 'publisher.users' doesn't exist
DESCRIBE `users`
Filename: /Library/WebServer/Documents/ci/libraries/Datamapper.php
Line Number: 645

I can get things to work for this simple test if I set the prefix in the config file, but obviously won't work for the other prefixes. I'm sure I'm doing something simple, but just can see it. Any ideas?
#99

[eluser]WanWizard[/eluser]
@Maglok,

Datamapper is about relations, it can't produce data in a relation that is not related. Wink

The best option is probably to retrieve all hobbies, and retrieve the person including it's related hobbies. Then iterate over the hobbies, and check if the id exists as a key in persons->hobbies->all. If so, that hobby is related, and if not, well...

[eluser]WanWizard[/eluser]
@tarciozemel,

Datamapper passes where() clauses unaltered to $this->db->where(). Maybe something changed in CI's DB drivers?

You can try
Code:
...
$companies->where('MATCH (foo) AGAINST ("bar" IN BOOLEAN MODE)', NULL, FALSE);
...
which will disable escaping...




Theme © iAndrew 2016 - Forum software by © MyBB