CodeIgniter Forums
[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) - 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: [Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) (/showthread.php?tid=18196)



[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) - El Forum - 08-04-2009

[eluser]OES[/eluser]
I think Ive gone just about mad.

Here is my code.

Code:
$c = new Uscode();
$c->select('city');
$c->like('city', $code, 'after');
$c->distinct();
$c->order_by('city', 'asc');
$c->get(10);

which produes the SQL I want:

Code:
SELECT DISTINCT `city`
FROM (`uscode`)
WHERE `city` LIKE 'h%'
ORDER BY `city` asc
LIMIT 10

If I do a simple foreach.

Code:
foreach($c->all as $city){
  echo $city->city."<br />";
}

It only displays 1 results.

Putting the code in normal SQL and looping works perfect.

But I have noticed after the query if I do

echo count($c->all);

It only give 1

But if I try a count to test results ie.

echo $c->count();

I get plenty of result numbers.

Any ideas what can be going on.

Hope you can advise
Its a simple model with no relationships


[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) - El Forum - 08-04-2009

[eluser]tdktank59[/eluser]
[quote author="OverZealous.com" date="1249438684"]@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.[/quote]

Code:
$problem->owner->get();
$problem->assigned_to->get();

Both those are the "users" table


[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) - El Forum - 08-04-2009

[eluser]OverZealous[/eluser]
@OES
You have to select the id as well.


[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) - El Forum - 08-04-2009

[eluser]tdktank59[/eluser]
Ah that would explain it wouldnt it lol

Nope...
Same issue with it like this

Code:
$problem->include_related('member',array('id','name'));
$problem->include_related('status', array('id','name'));
$problem->include_related('owner', array('id','first_name', 'last_name'));
$problem->include_related('assigned_to', array('id','first_name', 'last_name'));

[edit]
Woops not directed at me... but it made some sense lol


[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) - El Forum - 08-04-2009

[eluser]OverZealous[/eluser]
@tdktank59
Smile Yeah, I didn't realize there was more than one post when I submitted that, so I went back and added @OES quickly. (Netbooks have such tiny screens!)

You don't have to select the ID for include_related. You just need correctly configured $has_one relationships.

If you aren't getting an error, and it still isn't querying them, the only option I see is what I mentioned before — that you are somehow clearing the query before running it. Make sure you've looked inside function calls, these things can be insidious.

The marked lines of code below make no sense:
Code:
$problem = new Problem();
> $problem->member->get();
> $problem->status->get();
> $problem->owner->get();
> $problem->assigned_to->get();

You cannot get related items before you get $problem. What you've done here is generated and run 4 queries that look like this:
Code:
SELECT members.*
FROM members
LEFT OUTER JOIN members_problems on members_problems.member_id = members.id
LEFT OUTER JOIN problems on members_problems.problem_id = problems.id
WHERE problems.id = 0

Which just sets $problem->member to a new, empty Member (since I assume this returns no items).


[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) - El Forum - 08-05-2009

[eluser]jpi[/eluser]
@OES
I think you can only use a distinct clause with a group by clause. (not sure but try to add $c->group_by('city', 'asc');

@phil
I have tried for hours to alter a table to add a new column and to save a new property of my object in the new column (both operation in the same script).

Code sample :
Code:
$user = new User();
$user->get_by_id(1);
echo $user->login //This works
$this->db->query('ALTER TABLE `users` ADD `comment` VARCHAR( 255 ) NOT NULL');
$user->comment = 'my little comment';
$user->save();
$user->clear();
$user->get_by_id(1);
echo $user->comment //Empty string
This can't work because even method clear() doesn't clear datamapper::fields property (initialized line 52, populated line 189)

What i dont understand is why this doesn't work :
Code:
$user = new User();
$user->get_by_id(1);
var_dump($user->login); //This works
$this->db->query('ALTER TABLE `users` ADD `comment` VARCHAR( 255 ) NOT NULL');
unset($user);
$user = new User();
$user->get_by_id(1);
$user->comment = 'my little comment';
$user->save();
$user->clear();
$user->get_by_id(1);
var_dump($user->comment); //Empty string

I can't find any cache mechanism regarding
$this->db->field_data($this->table)

It would be nice if you have any clue about this. (i have already achieve what I attempted to do in another way)


[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) - El Forum - 08-05-2009

[eluser]OverZealous[/eluser]
[quote author="jpi" date="1249484621"]I think you can only use a distinct clause with a group by clause. (not sure but try to add $c->group_by('city', 'asc');
[/quote]

This is not true. Distinct works with any query (at least in most databases).

The error OES is or was having relates to how the original DataMapper handled query results (by storing them in the all array according to their id). I eventually plan on removing this and using a non associative array, since I think it hides a lot of bad queries. However, it is a significant change, that will most likely break sites, so I cannot commit to it until probably DMZ 2.0.

Quote:I have tried for hours to alter a table to add a new column and to save a new property of my object in the new column (both operation in the same script).

I need to make this clear, because it comes up occasionally:

DataMapper does not support dynamically modified databases. At all. And it most likely never will. Getting something like that to work is considered a hack, and may break in the future. No warranties. Tongue

DataMapper doesn't "cache" the fields, per se, but instead stores them in a statically shared array, one for each model. This prevents them from being loaded for each and every object, which in a large query would be just foolish. The clear method only clears properties that are unique to each object.

In general, I can't understand why you would ever want to dynamically change a database table. It's very inefficient, and doesn't even make sense in your code sample. (Why not change it manually?) If you need to change a table, it will need to be changed on one HTTP request, and then the changes will show up on the next HTTP request. Of course, this is only true if you are not using the Production Cache. It also is not guaranteed to not break at some point in the future.


[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) - El Forum - 08-05-2009

[eluser]naren_nag[/eluser]
Guys,

I need some help with this.

First my models

Code:
class User {

var $has_many = array( "authored_knowledge" => array( "class" => "knowledge", "other_field" => "author" ) );

}

class Knowledge {

var $has_one = array ( "author" => array("class" => "user", "other_field" => "authored_knowledge") );
var $has_many = array ( "group" );
}

class Group {
var $has_many = array ("knowledge");
}

So,
User has many Knowledge (as authored_knowledge)
Knowledge has one User (as author)

Knowledge has many Group
Group has many Knowledge

Now, what I want to do is for a particular user, I want to get a count of all the knowledge that has a relationship with any group.

Here's what I'm trying to do:

Code:
$userObject = new User($id);

$userObject->authored_knowledge->where_related('group', 'id >', 0)->count();

The problem with this query is that if a knowledge is related to multiple groups, it adds to the count.

So if my data look liked this:

knowledge_1 shared with group_1
knowledge_1 shared with group_2

knowledge_2 shared with group_1

knowledge_3 not shared

What I would like this query to return is 2. It returns 3.

Any clues?

cheers,

Naren


[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) - El Forum - 08-05-2009

[eluser]OverZealous[/eluser]
Naren,
Code:
$userObject->authored_knowledge->distinct()->where_related('group', 'id >', 0)->count();
                               ^^^^^^^^^^^^

Should work, not tested. ;-)


[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition) - El Forum - 08-05-2009

[eluser]bojack[/eluser]
I found a convenient solution for dealing with multiple databases, and since I have seen a few posts concerning this I thought I would share.

First, I edited

Code:
function _assign_libraries()
    {
        if ($CI =& get_instance())
        {
            $this->lang = $CI->lang;
            $this->load = $CI->load;
            $this->db = $CI->db;
            $this->config = $CI->config;
        }
    }

to:

Code:
function _assign_libraries()
    {
        if ($CI =& get_instance())
        {
            $this->lang = $CI->lang;
            $this->load = $CI->load;
            // use CodeIgniter's db if not set by model
            if (empty($this->db))
            {
                $this->db = $CI->db;
            }
            $this->config = $CI->config;
        }
    }

Then, in your model you can do:
Code:
public function __construct()
    {
        $CI =& get_instance();
        $this->db = $CI->load->database('other_database_key', TRUE);
        parent::__construct();
    }

Now you can use the models in your controller and not have to worry about unsetting and resetting $this->db, or caring where the database is.

This will of course fail all over if you are trying to relate an object from one DB to an object from another (I assume). But for my needs, it seems like the easy way to deal with a script that needs to read from multiple databases.