Looking to make a class that will be for 'customers'. The purpose of this class is to pass in some details, and then either add a user or update an existing one.
期待为“客户”创建一个课程。此类的目的是传递一些细节,然后添加用户或更新现有用户。
After this, I might also want to do to more things with this 'customer'.
在此之后,我可能还想与这个“客户”做更多的事情。
Im new to classes so need to know how to always refer to the same row no matter what i'm doing with the current initiated class.
我是新手,所以需要知道如何总是引用相同的行,无论我正在使用当前启动的类。
Basic use:
$customer = new Customer($customer_details);
When creating a new instance of this class, I want it to straight away add / update a row.
在创建此类的新实例时,我希望它能够立即添加/更新一行。
However, as well as this later on I might want to update a specific field:
但是,以及稍后我可能想要更新特定字段:
$customer->setValue('firstname','Craig');
But I dont want to have to do another lookup to get the row ID etc in this fucntion. I want $customer
to always reference the user given the $customer_info
.
但是我不想在这个功能中进行另一次查找来获取行ID等。我希望$ customer始终引用给定$ customer_info的用户。
2 个解决方案
#1
1
Assuming you want setValue()
to update the database, I would advise creating a field in your Customer
class that contains the identifier for the row in the database.
假设您希望setValue()更新数据库,我建议您在Customer类中创建一个包含数据库中行的标识符的字段。
/**
* Database identifier.
*
* @var string/int
*/
private $id;
Have your constructor set the value of the identifier on object creation using $this->id = $id
. Then your setValue()
call can use that identifier to update the information in the database.
让您的构造函数使用$ this-> id = $ id在对象创建时设置标识符的值。然后,您的setValue()调用可以使用该标识符来更新数据库中的信息。
EDIT
If you are creating a new Customer
instead of updating an existing one (passing new data to the constructor) the database controller that you are using should have a means to get the ID of the new row (given it is an auto-incremented value). Otherwise the ID is something that you are generating, in which case you could just set the field value in the class.
如果要创建新客户而不是更新现有客户(将新数据传递给构造函数),您使用的数据库控制器应该有一种方法来获取新行的ID(假设它是一个自动递增的值) 。否则,ID就是您正在生成的内容,在这种情况下,您只需在类中设置字段值即可。
#2
-1
I think you're referring to the Active Record Pattern. You can find many implementations in PHP, such as Propel.
我认为你指的是Active Record Pattern。您可以在PHP中找到许多实现,例如Propel。
#1
1
Assuming you want setValue()
to update the database, I would advise creating a field in your Customer
class that contains the identifier for the row in the database.
假设您希望setValue()更新数据库,我建议您在Customer类中创建一个包含数据库中行的标识符的字段。
/**
* Database identifier.
*
* @var string/int
*/
private $id;
Have your constructor set the value of the identifier on object creation using $this->id = $id
. Then your setValue()
call can use that identifier to update the information in the database.
让您的构造函数使用$ this-> id = $ id在对象创建时设置标识符的值。然后,您的setValue()调用可以使用该标识符来更新数据库中的信息。
EDIT
If you are creating a new Customer
instead of updating an existing one (passing new data to the constructor) the database controller that you are using should have a means to get the ID of the new row (given it is an auto-incremented value). Otherwise the ID is something that you are generating, in which case you could just set the field value in the class.
如果要创建新客户而不是更新现有客户(将新数据传递给构造函数),您使用的数据库控制器应该有一种方法来获取新行的ID(假设它是一个自动递增的值) 。否则,ID就是您正在生成的内容,在这种情况下,您只需在类中设置字段值即可。
#2
-1
I think you're referring to the Active Record Pattern. You can find many implementations in PHP, such as Propel.
我认为你指的是Active Record Pattern。您可以在PHP中找到许多实现,例如Propel。