Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] PHP ActiveRecord and CodeIgniter issue... [/SOLVED]
#1

[eluser]Unknown[/eluser]
Ok, So I have CodeIgniter and php-activerecord functioning or so I think..My issue is that I always get returned a null value.. My DB is setup like so...

Code:
CREATE TABLE IF NOT EXISTS `classes` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(5) NOT NULL,
  `class` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

INSERT INTO `classes` (`id`, `uid`, `class`) VALUES
(1, 3, 'Intro to VB'),
(4, 4, 'Introduction to the Internet'),
(3, 4, 'Introduction to the Internet'),
(5, 4, 'Introduction to the Internet'),
(6, 4, 'Introduction to the Internet'),
(7, 4, 'Introduction to the Internet'),
(8, 4, 'Introduction to the Internet'),
(9, 4, 'Introduction to the Internet'),
(10, 4, 'Introduction to the Internet'),
(11, 4, 'Introduction to the Internet');

-- --------------------------------------------------------

CREATE TABLE IF NOT EXISTS `notes` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `uid` int(5) NOT NULL,
  `cid` int(5) NOT NULL,
  `title` varchar(255) NOT NULL,
  `note` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

INSERT INTO `notes` (`id`, `uid`, `cid`, `title`, `note`) VALUES
(1, 0, 0, 'test note', 'this is a test note'),
(2, 3, 1, 'test note', 'this is a test note'),
(3, 4, 1, 'test note', 'this is a test note');

-- --------------------------------------------------------

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `email` varchar(75) NOT NULL,
  `password` varchar(128) NOT NULL,
  `isAdmin` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

INSERT INTO `users` (`id`, `email`, `password`, `isAdmin`) VALUES
(3, '[email protected]', 'hashed_pw', 1),
(4, '[email protected]', 'hashed_pw', 0);

When I request information from the database for the notes table using php-activerecord I get values back but they are all null.. It's connecting to my DB, reading the table information for the notes table then returns back null values...

Code:
return Notes::all();

OR

return Notes::find('all', array('order'=>'RAND()','limit'=>'1'));

Either return a null resultset. If I do a var_dump() on the values I can see the field names in the array..But They're marked NULL...

I dont quite get it...Here is my actual code

Code:
class Main extends CI_Controller {

    function __construct(){
        parent::__construct();
        
    }

    public function index()
    {    
        $this->load->spark('php-activerecord/0.0.1');
        $data = Notes::find('all', array('order'=>'RAND()','limit'=>'1'));
        echo "<pre>";
        foreach($data as $r) { var_dump($r->to_array()); }
        //print_r($data->to_array());
        echo "</pre>";
        $this->load->view('main_view', $data);
    }


    
    
}

Does anyone have any idea what the he** is going on?
#2

[eluser]Unknown[/eluser]
I know this is an old thread and 'solved' the problem, but I'd just like to point out that you can't just override the constructor and not pass the defaults if you want it to behave correctly.

This is incorrect:
Code:
public function __construct()
{
    parent::__construct();  
}
You're essentially replacing their object constructor code and saying to create an object with no attributes every single time.

The correct way to do this would be via:
Code:
public function __construct($attributes=array(), $guard_attributes=true, $instantiating_via_find=false, $new_record=true)
{
   parent::__construct($attributes,$guard_attributes,$instantiating_via_find,$new_record);  
}
Which in itself to me feels like a slight oversight by them as I'd prefer object construction via a factory method.. but meh.




Theme © iAndrew 2016 - Forum software by © MyBB