CodeIgniter Forums

Full Version: DMZ Problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]7amza[/eluser]
Hey,
i started using the DMZ library and all goes cute expet i'm encoutring a problem :
i have three tables :

portals : id - title..
pages : id - portal_id - title..
categories : id - portal_id - title ..

the relation between them is like that :
each page and category has one portal .
and each portal have many categories and pages .
so my models is like that:
:
Code:
//Portal.php
class Portal extends DataMapper {
    
    var $table = "portals";
    var $has_many = array("category","page");

    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
}

//Page.php
class Page extends DataMapper {

    var $table = "pages";
    var $has_one = array("portal");
    var $has_many = array();

    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
}

//Category.php
class Category extends DataMapper {

    var $table = "categories";
    var $has_one = array("portal");
    var $has_many = array("article");

    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
}
so in controllers when i use this method :
Code:
function cat(){
$category = new category();
        $data['categories'] = $category->get_where(array('portal_id'=>'2'));
it works fine and return all categories related to portal 2 .
when i use this method:
Code:
$page = new page();
        $data['portal'] = $page->get_where(array('portal_id'=>'2'));
the method don't works fine:
Quote:DataMapper Error: 'page' is not a valid parent relationship for Portal. Are your relationships configured correctly?
and when i try to get all categories and pages related to portal from the portal i can't gets them !
Code:
$portal = new portal(2);
$categories = $portal->category->get();
$pages = $portal->page->get();//Line 21
Quote:Fatal error: Call to a member function get() on a non-object in G:\AppServ\www\test\application\modules\dir\controllers\home.php on line 21
notice : the line 20 works fine alone it gets all categories related to portal 2 .
so what's wrong please?

El Forum

[eluser]J Maxwell[/eluser]
Your $has_xxx associations are wrong - it needs to reference the singular of the modelname - so 'page' rather than 'pages' like you have. Hope that helps,

John

El Forum

[eluser]7amza[/eluser]
Hey Max ,
in my code i wrote 'page' but still the problem exist ! (i corrected the code here too)
i don't know what's wrong evenif all code is logic like it is describe in the manual !!

El Forum

[eluser]J Maxwell[/eluser]
Does it work if you do:
Code:
$page = new page();
$p = $page->where_related_portal('id',2)->get();

Trying it from another angle? Do you possibly have a typo in the field in your database? The join field should be singular with _id at the end?

Is the code here copied and pasted?

El Forum

[eluser]7amza[/eluser]
i tested the code but it doesn't get a result :
Code:
function index()
    {
        $setting = new setting();
        $data['setting'] = $setting->get();
        $portal = new portal(2);
        $data['categories'] = $portal->category->get();
        $page = new page();
        $data['pages'] = $page->where_related_portal('id',2)->get();  
        $this->load->view('header',$data);
        $this->load->view('right',$data);
        $this->load->view('home',$data);
        $this->load->view('left',$data);
        $this->load->view('footer',$data);
        
        
    }
my database structure is :
Code:
CREATE TABLE `categories` (
  `id` int(11) NOT NULL auto_increment,
  `portal_id` int(11) default NULL,
  `title` varchar(255) NOT NULL,
  `description` varchar(600) NOT NULL,
  `slug` varchar(100) NOT NULL,
  `created` datetime NOT NULL,
  `updated` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;

CREATE TABLE `pages` (
  `id` int(11) NOT NULL auto_increment,
  `portal_id` int(11) default NULL,
  `title` varchar(255) NOT NULL,
  `content` longtext NOT NULL,
  `slug` varchar(100) NOT NULL,
  `created` datetime NOT NULL,
  `updated` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;

CREATE TABLE `portals` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(150) NOT NULL,
  `created` datetime NOT NULL,
  `updated` datetime NOT NULL,
  `slug` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `title` (`title`,`slug`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
to be clear the problem is just in the relationship between pages table and portals table , but i can't discover where is the problem !!

El Forum

[eluser]J Maxwell[/eluser]
Yeah, I can see. Well, I can't. But I'll keep thinking for you.

J

El Forum

[eluser]J Maxwell[/eluser]
Try changing this:

Code:
$portal = new portal(2);
        $data['categories'] = $portal->category->get();
        $page = new page();
        $data['pages'] = $page->where_related_portal('id',2)->get();

To this:

Code:
$portal = new portal(2);
        $data['categories'] = $portal->category->get();
        $data['pages'] = $portal->pages->get();

Since you can access categories from portal, try originating it from there as well?

El Forum

[eluser]7amza[/eluser]
thank you J ,
really i'm so confused hope someone can help me .

El Forum

[eluser]7amza[/eluser]
Quote:and when i try to get all categories and pages related to portal from the portal i can't gets them !
Code:
$portal = new portal(2);
$categories = $portal->category->get();
$pages = $portal->page->get();//Line 21
Quote:Fatal error: Call to a member function get() on a non-object in G:\AppServ\www\test\application\modules\dir\controllers\home.php on line 21
SadSadSad

El Forum

[eluser]7amza[/eluser]
any reply guys?
Pages: 1 2