Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检索其表属于某个模型的所有字段,并使用该模型的对象引用访问这些字段?_Php_Model View Controller_Laravel 4 - Fatal编程技术网

Php 如何检索其表属于某个模型的所有字段,并使用该模型的对象引用访问这些字段?

Php 如何检索其表属于某个模型的所有字段,并使用该模型的对象引用访问这些字段?,php,model-view-controller,laravel-4,Php,Model View Controller,Laravel 4,我想用PHP开发专有的MVC框架,并以非常清晰的方式理解OOP概念。我就在这里。首先看一下代码片段 // main model class class Model{ protected static $table; protected static $primary_key; protected static $conn; public function __construct() { // variable calling from c

我想用PHP开发专有的MVC框架,并以非常清晰的方式理解OOP概念。我就在这里。首先看一下代码片段

// main model class
class Model{
    protected static $table;
    protected static $primary_key;
    protected static $conn;

    public function __construct()
    {
        // variable calling from configuration file
        global $defalult_database_engine,$connections;
        self::dbConnection();
        // query to fetch all columns name belo
        $query="SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA =? AND TABLE_NAME =?";
        $stmt=self::$conn->prepare($query);
        $stmt->execute(array($connections['mysql']['database'],'users'));
        $fields=$stmt->fetchAll(PDO::FETCH_OBJ);
        foreach($fields as $field)
        {
            $fileldname=$field->COLUMN_NAME;
            // creating variable name to matching to the tables fields name
            // how to set value of this variable via object
            $$fileldname;
        }
    }
儿童模型是这样的

class Users extends Model
{
    protected static $table='users';
}
现在转向控制器

class UserController extends Controller{
     public function __construct(){
     }

     public function createUser(){
        // user model
        $user=new Users();
        // calling attributes of the table and set their value
        $user->name='full name';
        $user->user_name='user name';
        $user->password='password';
        // finally save the value of fields
        $user->save();            
     }
}

我想在上面的时尚工作。我将表的字段名转换为变量,但无法通过其对象引用它。。。。与上面给出的UserController中的方式完全相同。有什么办法使之成为可能吗?事实上,我目前在Laravel 4.2中工作,并受其影响

我看到你试图重新创建拉威尔的语法——听起来像是一个有趣的项目

Laravel利用PHP创建模型属性数组

以下是Laravel的get和getAttribute方法的源代码:

public function __get($key)
{
    return $this->getAttribute($key);
}
public function getAttribute($key)
{
    if (array_key_exists($key, $this->attributes) || $this->hasGetMutator($key)) {
        return $this->getAttributeValue($key);
    }

    return $this->getRelationValue($key);
}

我看到你在尝试重新创建Laravel的语法——听起来像是一个有趣的项目

Laravel利用PHP创建模型属性数组

以下是Laravel的get和getAttribute方法的源代码:

public function __get($key)
{
    return $this->getAttribute($key);
}
public function getAttribute($key)
{
    if (array_key_exists($key, $this->attributes) || $this->hasGetMutator($key)) {
        return $this->getAttributeValue($key);
    }

    return $this->getRelationValue($key);
}

不需要从表中检索字段详细信息;我们必须利用动态实例变量,而不是检索;它可以像下面的代码一样在php中生成

$user->user_name; // $user_name variable has been created dynamically and bolongs to the $user table
$user->user_name='your user_name'; // value assigned to dynamic instance variable
$user->save();// this function is defined to the model class
主模型save()的代码如下:

public function save()
{
   $datainfo = (array)$this // this assing the array to the $datainfo variable with all dynamic instance variable
   // do your manipulation with data received in $datainfo
}  

不需要从表中检索字段详细信息;我们必须利用动态实例变量,而不是检索;它可以像下面的代码一样在php中生成

$user->user_name; // $user_name variable has been created dynamically and bolongs to the $user table
$user->user_name='your user_name'; // value assigned to dynamic instance variable
$user->save();// this function is defined to the model class
主模型save()的代码如下:

public function save()
{
   $datainfo = (array)$this // this assing the array to the $datainfo variable with all dynamic instance variable
   // do your manipulation with data received in $datainfo
}  

非常感谢您的评论。。。你觉得这很有趣,但对我来说提高自己是至关重要的。。最终我找到了解决办法;解决方案确实很简单,但很难想象这样熟悉其他编程语言的时尚人士$user->user\u name//创建一个动态实例变量$instances=(array)$this//此代码将您的对象转换为arrayGlad以提供帮助。如果您觉得它有帮助,请向上投票或将其标记为完整。感谢并祝愿您的项目和个人成长!不管它对我有没有帮助,我都会投它一票;重要的是你有多大的帮助。谢谢sirji。非常感谢你的评论。。。你觉得这很有趣,但对我来说提高自己是至关重要的。。最终我找到了解决办法;解决方案确实很简单,但很难想象这样熟悉其他编程语言的时尚人士$user->user\u name//创建一个动态实例变量$instances=(array)$this//此代码将您的对象转换为arrayGlad以提供帮助。如果您觉得它有帮助,请向上投票或将其标记为完整。感谢并祝愿您的项目和个人成长!不管它对我有没有帮助,我都会投它一票;重要的是你能帮上什么忙谢谢sirji。