dsvjw20866 2019-04-14 10:03
浏览 50
已采纳

如何使用PHP / MySQL实现良好的MVC模式而不会丢失SQL请求时间/服务器内存? (良好做法)

I want to implement a real pattern MVC for my php controllers. Especially, i want to split Model and API by creating java "bean" equivalent in PHP (objects made for business organization) and an API using these business objects.

For exemple, my basic object is the Member. The question is : where i request my database ? Do I request all the members proprities right at __construct, and i simply access them with the getters OR i do nothing in the __construct and I call the database in every getter function ? People tell me that the 1st solution is better, though, if i want only a specific information in my controller, i will create a Member with all the informations computed right at construct (bad memory management). In the 2nd case, if i want several members proprities, i will do several SQL request which will increase my server execution time.

1st solution :

public function __construct($ID_membre,$sql)
{
    $this->ID_membre = $ID_membre;
    $res = mysql_get("select * from membres where ID_membre = $ID_membre",$sql);
    if(!$res)
        throw new Exceptions\MyDefaultException("no member for this Id");

    $this->firstName = $res['first_name'];
    $this->lastName = $res['last_name'];
    $this->mail = $res['mail'];
    $this->gender = $res['gender'];
    // ....

    $this->sql = $sql;
}
public function getLastName()
{
    return $this->lastName;
}
public function getMail()
{
    return $this->mail;
}
public function getGender()
{
    return $this->gender;
}
// .... 

2nd solution :

public function __construct($ID_membre,$sql)
{
    $this->ID_membre = $ID_membre;
    $res = mysql_get("select count(*) from membres where ID = $ID_membre",$sql);
    if($res == 0)
        throw new Exceptions\MyDefaultException("no member with this id");



    $this->sql = $sql;
}
public function getLastName()
{
    mysql_get("select name from members where ID = {$this->id}",$this->sql);
    return $this->lastName;
}
public function getMail()
{
    mysql_get("select mail from members where ID = {$this->id}",$this->sql);
    return $this->mail;
}
public function getGender()
{
    mysql_get("select gender from members where ID = {$this->id}",$this->sql);
    return $this->gender;
}

In this context, good old SQL custom request within controllers are perfect to not waste time or memory, because they are customs. So, why doing such request are so poorly viewed nowsaday ? And if big organizations such as Fb or Google do MVC with database, how they manage to not waste any time/memory while splitting Model and controllers ?

  • 写回答

1条回答 默认 最新

  • dsc862009 2019-04-14 10:15
    关注

    This is a classic problem, which can even get worse if you want one property of many members.

    The standard answer is that solution 1 is better. Requesting one row from a database doesn't take much longer than requesting one value from a database, so it makes sense to ask a whole row at once. That is unless your database rows get very big. That should however not occur in good database design. If your rows get so big that they hamper efficiency then it is probably time to split the table.

    Now back to the problem I mentioned at the start of this answer. You haven't solved this. My suggestion would be to make two classes: One with solution 1, dealing with one row, and one with solution 2 dealing with multiple rows.

    So both solutions have their place, it just that solution 2 is almost always inefficient for dealing with one row, and I haven't even talked about the amount of extra coding it requires.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?