• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with returned from the database in class

#1
Hello. CodeIgniter 3.1.3 problem in the following


PHP Code:
class User_lib {
protected 
$customer_id;
protected 
$email;
protected 
$firstname;
protected 
$date_added;



model

PHP Code:
public function get_customer()
{
$this->db->from('stels_customer');
$this->db->where('stels_customer.customer_id'1);
$query $this->db->get();
return 
$query->row(0'User_lib');



controller

PHP Code:
public function index()
{
$this->load->model('user_model');
$this->load->library('user_lib');
var_dump($this->user_lib);
$user $this->user_model->get_customer();
var_dump($user);



results

PHP Code:
D:\Ampps\www\igniter\application\controllers\Welcome.php:26:
object(User_lib)[17]
protected 
'customer_id' => null
protected 'email' => null
protected 'firstname' => null
protected 'date_added' => null 



PHP Code:
D:\Ampps\www\igniter\application\controllers\Welcome.php:30:
object(User_lib)[20]
protected 
'customer_id' => string '1' (length=1)
protected 
'email' => string '[[email protected]][email protected][/email]' (length=16)
protected 
'firstname' => string 'Имя' (length=6)
protected 
'date_added' => string '2017-02-07 08:48:10' (length=19


Why are set value to non-public properties.

In guide:
The object will have all values returned from the database set as properties. If these have been declared and are non-public then you should provide a __set() method to allow them to be set.

Sorry for my English
Reply

#2
Please explain what the problem is.
Reply

#3
And use code tags.
IgnitedCMS Pro
IgnitedCMS
Practical guide to IgnitedCMS - Book coming soon
Reply

#4
You were asking why the object properties are not public.
Well, maybe because you declared them as protected in the User_lib class yourself?
PHP Code:
class User_lib {
protected 
$customer_id;
protected 
$email;
protected 
$firstname;
protected 
$date_added;


If properties are public, you can set them anywhere in your application like this:
PHP Code:
$this->User_lib->email '[email protected]'

If properties are private or protected, you need a __set() method inside your object to change the value. Like this:
PHP Code:
   public function __set($name$value)
 
   {
 
       $this->$name $value;
 
   

This is a so-called magic method. It will be automatically called when you try to change the value of a non-public property.
Reply

#5
The problem in the next, is not called __set method if set private or protected propeties.
Yet I have done so:

PHP Code:
class User_lib {
protected 
$_customer_id;
protected 
$_email;
protected 
$_firstname;
protected 
$_date_added;


And __set method now work
Reply

#6
Glad to hear that it's working now (???)
Reply

#7
(02-27-2017, 01:42 PM)Wouter60 Wrote: Glad to hear that it's working now (???)

Working, working))))) I still can not understand why the values are set to the closed variable if their name matches the name of the field in the database
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.