CodeIgniter Forums
DataMapper ORM v1.8.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 ORM v1.8.0 (/showthread.php?tid=37531)



DataMapper ORM v1.8.0 - El Forum - 02-10-2011

[eluser]Basketcasesoftware[/eluser]
Code:
$modules = new Modules();

$modules->where_related("groups/permissions",...
And at the "..." is where I get stuck because you name the tables but don't give their structure.
But what you are looking for in the DataMapper User Guide is Get (Advanced) under the head Deep Relationship Queries.

Your example uses a framework where the tables have foreign keys.

[quote author="Robert Liljedahl" date="1297368772"]I'm stuck on howto make a query.
I got groups, permissions and modules. I'm working on the update-function in the groups-controller.
I want to list all the modules and show if the group do in fact have access to the module in question.

It's easy for me to get all modules. It's also easy for me to get all permissions for a certain group.
The question is how I combine these two into on query for DataMapper.

This is how I'd do it with a 'normal' query:

Code:
SELECT modules.*, permissions.id AS 'permission_id'
  FROM modules
    LEFT OUTER JOIN permissions
      ON (permissions.module_id = modules.id AND permissions.group_id = 4)
  GROUP BY modules.id
[/quote]


DataMapper ORM v1.8.0 - El Forum - 02-10-2011

[eluser]cahva[/eluser]
[quote author="momsenmeister" date="1294973337"]Hi,

I try using language lines for the validation, but I get the following error:
Quote:Parse error: syntax error, unexpected '(', expecting ')' in /var/www/virtual/beazy.org/htdocs/system/application/models/event.php on line 13

The language library and the helper are autoloaded and work fine everywhere else.

Anyone knows how to solve this?

Thx!

Code:
<?php

class Event extends DataMapper {
    
    var $has_many = array('user');

    var $validation = array(
        array(
            'field' => 'title',
            'label' => lang('wording_title'),
            'rules' => array('required'),
        ),
        array(
            'field' => 'date',
            'label' => lang('wording_date'),
            'rules' => array('required'),
        )
    );

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

/* End of file event.php */
/* Location: ./application/models/event.php */
[/quote]

Shouldnt this be like the manual says(Im suprised that WanWizard didnt suggest this Smile )
Code:
class Event extends DataMapper {
    
    var $has_many = array('user');

    var $validation = array(
        array(
            'field' => 'title',
            'label' => 'lang:wording_title',
            'rules' => array('required'),
        ),
        array(
            'field' => 'date',
            'label' => 'lang:wording_date',
            'rules' => array('required'),
        )
    );

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



DataMapper ORM v1.8.0 - El Forum - 02-11-2011

[eluser]Basketcasesoftware[/eluser]
How do I create and use DataMapper data objects in a library? Controllers are easy, just
Code:
$user= new User();

But would
Code:
class Somelibrary
{
    protected $ci;
    protected $user;
    
    public function __construct()
    {
        $this->ci =& get_instance();
        $this->user = new $this->ci->User();
    }
}
also work?

A solution would be a great addition to the manual as well.


DataMapper ORM v1.8.0 - El Forum - 02-11-2011

[eluser]Basketcasesoftware[/eluser]
My rough test of the above failed. I need a way to access the DataMapper objects from within a library class. Any ideas?


DataMapper ORM v1.8.0 - El Forum - 02-11-2011

[eluser]cahva[/eluser]
Have you tried to use it like you do in controller? Maybe not, because it should and would work Smile


DataMapper ORM v1.8.0 - El Forum - 02-11-2011

[eluser]Basketcasesoftware[/eluser]
You mean like $this->$user = new User(); ?
Yep, I tried it.
This is my test library class:
Code:
class Tester
{
    protected $user;
    protected $ci;
    
    public function __construct()
    {
        $this->ci =& get_instance();
        
        $this->user = new $User();
    }
}
...Doh!
Edit: After deleting the $ next to 'User' I still get a message like this:
Fatal error: Class 'User' not found in C:\xampp\htdocs\basicinstall\application\libraries\Tester.php on line 12
I have tried for accessing the class:
$this->ci->User();
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Testing::$User

Filename: libraries/Tester.php

Line Number: 12

Fatal error: Class name must be a valid object or a string in C:\xampp\htdocs\basicinstall\application\libraries\Tester.php on line 12
$this->ci->DataMapper->User();
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Testing::$DataMapper

Filename: libraries/Tester.php

Line Number: 12
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/Tester.php

Line Number: 12

Fatal error: Class name must be a valid object or a string in C:\xampp\htdocs\basicinstall\application\libraries\Tester.php on line 12
$this->User();
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Tester::$User

Filename: libraries/Tester.php

Line Number: 12

Fatal error: Class name must be a valid object or a string in C:\xampp\htdocs\basicinstall\application\libraries\Tester.php on line 12

Does it matter that the model is in an HMVC module? I don't know. That's going to be my next test. Sad


DataMapper ORM v1.8.0 - El Forum - 02-11-2011

[eluser]Basketcasesoftware[/eluser]
Nope. Moving them to the standard application locations and running the test library gives the same set of error messages. Not a consequence of HMVC at all that I can tell. Yes, I know I can create a DataMapper object in a control and pass it to a library function. But means, literally I will have to do that to EVERY controller and controller function because this is part of a authorization system. Not feasible. The class has to be instantiated in the library function. Sad


DataMapper ORM v1.8.0 - El Forum - 02-12-2011

[eluser]WanWizard[/eluser]
[quote author="cahva" date="1297383211"]
Shouldnt this be like the manual says(Im suprised that WanWizard didnt suggest this Smile )[/quote]
I've been very busy lately, you have beaten me to it... Smile


DataMapper ORM v1.8.0 - El Forum - 02-12-2011

[eluser]WanWizard[/eluser]
@Basketcasesoftware,

I have added this to the welcome controller's index method (using a spanking new CI2 install):
Code:
$this->load->library('datamapper');
$this->load->library('test');
$this->test->dmload();

The test library looks like this:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test
{
    function dmload()
    {
        $dmobj = new Dmobj();
        var_dump($dmobj);
    }
}

Which loads the model as advertised.

Are you sure the Datamapper library is loaded when you attempt to load the model? If not, the autoloader is not active, which would be the only reason I can think of why it would not work...

Datamapper objects are not CI objects, so there is no $this, $this->ci or $this->datamapper involved in accessing them.

edit: if the model is in an HMVC module, the module must be loaded/used before you can load models from it. Datamapper uses the loader array $_ci_model_paths to determine where to load models from. Modular CI does this when you make the module known to the application (using $this->load->module), Modular Extensions via the _add_module_paths() method which is called when the module is loaded. This implies that you can only autoload module models from within the module. You can work around that by adding the path manually, using
Code:
$this->load->_add_module_paths('mymodule');



DataMapper ORM v1.8.0 - El Forum - 02-12-2011

[eluser]Basketcasesoftware[/eluser]
Doh! I knew had to be something obvious. I don't know where I got the impression that DataMapper autoloaded.

Ok. I'll be testing that.