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-17-2009

[eluser]tdktank59[/eluser]
Nothing...

It creates the entry, setups the data_source relation (with join_table) and dosn't do anything else...

I modified the values in the dqips insert. They are all valid inputs in the original version. So don't worry about that issue.

Quote:0.0004 SELECT * FROM `dqips` LIMIT 1

0.0003 SELECT *
FROM (`dqips`)
WHERE `title` = 'Titlie'
LIMIT 1

0.0006 INSERT INTO `dqips` (`version`, `title`, `description`, `demographics`, `impact_to_organization`, `automation_options`, `resolution_approach`, `re_extraction_schedule`, `conversion_impact`, `presented_date`, `created_on`, `estimated_completion_date`, `estimated_count_auto`, `estimated_count_manual`, `estimated_count_not_valid`) VALUES ('version', 'title', 'description', 'demographics', 'impact_to_organization', 'automation_options', 'resolution_approach', 're_extraction_schedule', 'conversion_impact', 'presented_date', 'created_on', 'estimated_completion_date', 'estimated_count_auto', 'estimated_count_manual', 'estimated_count_not_valid')

0.0006 SELECT *
FROM (`dqips`)
WHERE `id` = 1

0.0001 SELECT * FROM `data_sources` LIMIT 1

0.0001 SELECT *
FROM (`data_sources`)
WHERE `id` = 2

0.0003 SELECT *
FROM (`join_data_sources_dqips`)
WHERE `dqip_id` = '1'
AND `data_source_id` = '2'



DataMapper 1.6.0 - El Forum - 02-17-2009

[eluser]OverZealous[/eluser]
I know this is ugly, but is it possible to add this to datamapper.php line 2529 (after $related_properties is set), and send me the output?

update: stupid brain - meant $related_properties

Code:
echo("$related_field => {$object->model}");
var_dump($related_properties);

I'm trying to track down where this could be causing an issue, and I think it might somehow be related to the saving an array of data (I haven't used that much), but it's hard to tell where the code is stopping.

Update:
I've recreated your code, at least somewhat, and cannot find any place where it fails :-S .

However, I did come across one somewhat cool item. If you are saving multiple related objects that all have the same related field, you have to specify the save like this:
Code:
$object->save( array(
    'user' => array(
        $user1,
        $user2
    );
));
// alternately
$object->save( array($user1, $user2), 'user'); // this only works when saving just users.
This is because you can (obviously) only have one item per key in an array, so you have to specify the the items as an array. What I like is that actually reflects the actual relationship pretty well.


DataMapper 1.6.0 - El Forum - 02-17-2009

[eluser]tdktank59[/eluser]
data_source => data_sourcearray(4) { ["class"]=> string(11) "data_source" ["join_self_as"]=> string(4) "dqip" ["other_field"]=> string(4) "dqip" ["join_other_as"]=> string(11) "data_source" }

thats it thats new


DataMapper 1.6.0 - El Forum - 02-17-2009

[eluser]OverZealous[/eluser]
Note: the above issue has been resolved, it is not a DataMapper bug.


DataMapper 1.6.0 - El Forum - 02-17-2009

[eluser]tdktank59[/eluser]
[quote author="OverZealous.com" date="1234931885"]Note: the above issue has been resolved, it is not a DataMapper bug.[/quote]

It was my bug... Had to setup the users as objects not values...

(totally spaced on this)

Thanks for the help


DataMapper 1.6.0 - El Forum - 02-17-2009

[eluser]tdktank59[/eluser]
How exactly do you call the related fields now?

$asd = new Dqip();
$asd->where('id',$dqip_id)->get();
$asd->data_source->get();
$asd->author->get();

once I call the author i get an error since the join table does not exist.

Quote:Error Number: 1054

Unknown column 'join_dqips_users.dqip_author_id' in 'on clause'

SELECT `users`.* FROM (`users`) LEFT JOIN `join_dqips_users` as join_dqips_users ON `users`.`id` = `join_dqips_users`.`user_id` LEFT JOIN `dqips` as dqips ON `dqips`.`id` = `join_dqips_users`.`dqip_author_id` WHERE `dqips`.`id` = '1'



DataMapper 1.6.0 - El Forum - 02-17-2009

[eluser]OverZealous[/eluser]
It should work. I hate to say it, but it should. The _get_related_table method (line 2693) looks through the $fields array on each object. If (in your example) the field 'author_id' exists on the dquips table, it uses dquips for the join table.

The only reason that it should go to the old style join table is if 'author' is not set in the has_one array and 'author_id' is not a field in the table.


DataMapper 1.6.0 - El Forum - 02-17-2009

[eluser]tdktank59[/eluser]
[quote author="OverZealous.com" date="1234935824"]It should work. I hate to say it, but it should. The _get_related_table method (line 2693) looks through the $fields array on each object. If (in your example) the field 'author_id' exists on the dquips table, it uses dquips for the join table.

The only reason that it should go to the old style join table is if 'author' is not set in the has_one array and 'author_id' is not a field in the table.[/quote]

Allright ill take a look at it when I get a chance...

Kinda odd I havnt changed anything besides the objects for the users...


DataMapper 1.6.0 - El Forum - 02-18-2009

[eluser]miau[/eluser]
Hi,

first of all I'm really impressed with the lib. At least something like django's DB interface in PHP Smile

I have a problem with using where_related. I have Site table with urls and Tag with tags for sites. This is a many-to-many relation and looks like this:

Code:
class Site extends DataMapper {
    var $table = 'sites';    
    var $has_many = array('tag');
    
    function Site()
    {
        parent::DataMapper();
    }            
}
[...]

class Tag extends DataMapper {
    var $table = 'tags';    
    var $has_many = array('site');    

    function Tag()
    {
        parent::DataMapper();
    }                                  
}

Now I want to fetch all sites tagged with 'PHP' and 'CodeIgniter', so I do sth like this:

Code:
$site = new Site();
  $tag_1 = new Tag();
  $tag_1->get_by_tag('PHP');

  $tag_2 = new Tag();
  $tag_2->get_by_tag('CodeIgniter');
        
  $site->limit(30)->where_related($t1)->where_related($t2)->get();

But I don't get any results as the SQL WHERE section of the query generated by DataMapper will always return false:

Code:
SELECT `sites`.*
  FROM (`sites`)
    LEFT JOIN `sites_tags` ON `sites`.`id` = `sites_tags`.`site_id`
    LEFT JOIN `tags` ON `tags`.`id` = `sites_tags`.`tag_id`
      WHERE `tags`.`id` = '42' AND `tags`.`id` = '64'

Is there any way to do this in DataMapper or do I have to create the query manually?

thanks for help,
miau


DataMapper 1.6.0 - El Forum - 02-18-2009

[eluser]OverZealous[/eluser]
I did some research. The kind of query you are wanting is an intersection, which is fairly advanced for an ORM. However, I think it is pretty cool, too, so I'm thinking I might add it to my version of DataMapper.

Until this becomes part of the core, you can try hand doing it like this:
Code:
// NOT tested:
$tags = new Tag();
$tags->where_in("tag", array("PHP", "CodeIgniter")->get();
$sql = "tags.id IN (";
$first = TRUE;
foreach($tags->all as $tag)
{
    if($first)
    {
        $first = FALSE;
    }
    else
    {
        $sql .= "\nINTERSECT\n";
    }
    $sql .= 'SELECT sites_tags.tag_id FROM sites_tags WHERE sites_tags.site_id = ' . $tag->id;
}
$site->where($sql);

If you do this a lot, you can easily make it a method.

Update:
You poor MySQL users. I had no idea. I just discovered that MySQL doesn't support intersects. Given that, there is no way, that I can see, that someone using a MySQL database can get the results requested above, without resorting to multiple queries.