本文介绍了在对象中抽象SQL语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在寻找一些让我抽象数据库的评论

更新一个对象。


Lemme说明我在想什么:


让我说我有一个对象的人...


SetFirstName()

SetLastName()





(基本上目标是开发团队使用这些对象和

不要尝试自己写入数据库。)


好​​吧,我想避免的是当我调用SetFirstName()然后

调用SetLastName()执行两个UPDATE语句。我想

将它们全部组合成一个UPDATE或INSERT,所以我没有在数据库上击败




我想我需要将更改存储到本地的变量中,然后可能调用一个SendSQL()函数或类似

来生成SQL更改数据到数据库。


类似于:


$ DB->查询(" BEGIN TRANSACTION&qu​​ot;);


//人们用$ DB傻瓜

$ Person-> SetFirstName(Something);

$ Person- > SetLastName(" Something");

$ Person-> SendSQL();


//另一个玩弄$ DB的对象

$ AnotherOBj-> SomeMethod(" Foo");

$ AnotherOBj-> SendSQL();


$ DB - >查询(" COMMIT TRANSACTION&qu​​ot;);


无论如何 - 欢迎提出意见和经验!

解决方案





I am looking for comments on something that lets me abstract database
updates in an object.

Lemme explain what I am thinking:

Lets say I have an object Person with...

SetFirstName()
SetLastName()

etc.

(Basically the goal is for the development team to use the objects and
not try to write to the database themselves.)

Well, what I am trying to avoid is when I call SetFirstName() and then
call SetLastName() the execution of two UPDATE statements. I would like
to combine it all into one single UPDATE or INSERT so I am not beating
up on the database.

I am thinking I would need to store the changes into variables local to
the class and then maybe calling a SendSQL() function or something like
that to generated SQL changes of the data to the database.

Something like:

$DB->query("BEGIN TRANSACTION");

// Person fools around with $DB
$Person->SetFirstName("Something");
$Person->SetLastName ("Something");
$Person->SendSQL();

// Another object fooling around with $DB
$AnotherOBj->SomeMethod ("Foo");
$AnotherOBj->SendSQL();

$DB->query ("COMMIT TRANSACTION");

Anyhow - opinions and experiences are welcome!

解决方案




这篇关于在对象中抽象SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:36