Welcome Guest, Not a member yet? Register   Sign In
DMZ 1.7.1 (DataMapper OverZealous Edition)

[eluser]OverZealous[/eluser]
@The Hamburgler
Yup, yer doin' it wrong, should be:
Code:
$contact->select_func('SUBSTRING', '@last_name', 1, 1, 'letter')->group_by('letter')->get();

Also, if you are still having trouble, please see the note on the functions page about modifying the protect identifiers method.

[eluser]Tom Vogt[/eluser]
Hm, I still have this problem, with the added information that items belonging to user A can be at locations belonging to user B, which is why the first posted solution won't work.

[quote author="Tom Vogt" date="1279059516"]I'm looking for a way to access multiple relationships.

Here's my problem:

I have users, assets and items.

A user has many assets and many items.
An asset belongs to one user, and can contain many items.
Every item belongs to one asset and one user.

I want to find the items belonging to a specific user and a specific asset. Or, in other words, my SQL query would be:
Code:
select * from items where user_id=123 and asset_id=456

For added complexity, I only know the user via the asset, so I'm processing assets, and the user is $asset->user.
[/quote]

so $asset->user gives me the user of this asset. What I want is all the items of this user located at this asset. However, there can be other items of other users here as well, I don't want them.

[eluser]OverZealous[/eluser]
@Tom Vogt

Code:
$item = new Item();
$item->where_related('asset', 'id', $asset_id);
$item->where_related('user', 'id', $user_id);
$item->get();

[eluser]Tom Vogt[/eluser]
[quote author="OverZealous" date="1279388915"]@Tom Vogt
Code:
$item = new Item();
$item->where_related('asset', 'id', $asset_id);
$item->where_related('user', 'id', $user_id);
$item->get();
[/quote]

That makes sense, it's basically the DMZ equivalent of the SQL query. It's probably a ton easier than going through the relationship hierarchy. Thanks!

[eluser]WanWizard[/eluser]
@phil,

I'm using the new CI 2.0 packages as a way to introduce modulairity without complicated extensions like HMVC or Matchbox. As DMZ 1.7.1. already supports the package paths, this works quite well.

However, not every module has models. the method recursive_require_once() chokes on this, as it assumes that the given path exists. I suggest changing it to:
Code:
protected static function recursive_require_once($class, $path)
{
    $found = FALSE;
    if ( is_dir($path) )
    {
        $handle = opendir($path);
        if ($handle)
        {
            while (FALSE !== ($dir = readdir($handle)))
            {
                // If dir does not contain a dot
                if (strpos($dir, '.') === FALSE)
                {
                    // Prepare recursive path
                    $recursive_path = $path . '/' . $dir;

                    // Prepare file
                    $file = $recursive_path . '/' . $class . EXT;

                    // Check if file exists, require_once if it does
                    if (file_exists($file))
                    {
                        require_once($file);
                        $found = TRUE;

                        break;
                    }
                    else if (is_dir($recursive_path))
                    {
                        // Do a recursive search of the path for the class
                        DataMapper::recursive_require_once($class, $recursive_path);
                    }
                }
            }

            closedir($handle);
        }
    }
    return $found;
}
to avoid getting Opendir errors when the path does not exist.

[eluser]OverZealous[/eluser]
@WanWizard
Thanks! I'll include that fix when I next get some time to work on DMZ.

[eluser]Skipper[/eluser]
Not really a problem, but a wish for cosmetic reasons/readability: I am looking to have some sort of pseudo fields, ie. fields that are handled within the model, but who do not exist in the database.

If I have database fields 'quantity_on_stock' and 'cost_per_unit', I thought that having a getter would be nice that goes like
Code:
public function get_inventoryvalue()
{
  return $this->quantity_on_stock * $this->cost_per_unit;
}

Of course this could be easily done with functions, but I assume that not dealing with () makes it easier to differentiate function(ality) and field information.

Any tricks how to accomplish this?

Thanks,
Skipper

[eluser]OverZealous[/eluser]
@Skipper
This isn't specifically related to DMZ, but try the magic __get method: it's used throughout DMZ to handle relationships and more.
Code:
// in *your* class
public function __get($field) {
    if($field == 'inventoryvalue') {
        return $this->quantity_on_stock * $this->cost_per_unit;
    }
    // important: defer to DMZ!
    return parent::__get($field);
}

Then you can access it like a field.
Code:
echo $object->inventoryvalue;

If you reference the value a lot, you may want to cache it, by setting $this->inventoryvalue to the result of the multiplication. (The magic __get method is only called if the field does not already exist on the object.)

One more thing: If you want the field to show up in your IDE (assuming you use one), put this in the class's PHPDoc comment:
Code:
/**
* ...
*
* @param float $inventoryvalue
*/
class MyClass extends DataMapper {
...

[eluser]bEz[/eluser]
Hey Phil,

It's been a minute since I've needed your assistance with DMZ technical issues, but as I can see, you're still providing the most informational, advantageous, superdicular, "feel free to stop me when you can," support I've ever seen.

I believe that between this thread and the legacy thread, if someone were to read each and every post, it would serve as an alternative approach to learning advanced PHP5 solutions (as related to CodeIgnter) and how to provide Technical Support (beyond the typical "Read the effing manual!" statement).

- bEz

[eluser]OverZealous[/eluser]
Thanks for the compliment, bEz!

And, lookie here, this is my 1000th post on these forums! Holy ****! :coolsmile:




Theme © iAndrew 2016 - Forum software by © MyBB