使用Yii框架的数据库功能 [英] Using the database features of the Yii framework

查看:124
本文介绍了使用Yii框架的数据库功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我开始用Yii的PHP MVC框架的工作。我在寻找建议我应该怎么继续通过框架使用的数据库:我应该使用框架的基础类的CActiveRecord其中涉及数据库,或者我应该去与经典的SQL​​查询功能(在我的情况MSSQL)<? / P>

显然与否,对我来说似乎更容易通过经典的SQL​​查询来处理数据库,但是,在某些时候,我想必须有使用框架的方法的优势。 一些SQL查询将获得pretty的复杂pretty的频繁。我只是不能COM prehend框架如何能帮助我,而不是使事情更加复杂比实际。

解决方案

这是我的经验与Yii的和大规模的数据库非常普遍的规则:

使用Yii的活动记录时:

  1. 您想要检索和后一个数据库中的几行(例如用户改变他/她的设置,更新用户的平衡,增加一票,让用户的计数在线,让下一个话题的帖子的数量,检查如果模型存在)
  2. 您想快速设计层次模型结构的表,(如:$用户可>信息 - >电子邮件,$用户可>设置 - >货币)之间,让您迅速调整每次使用显示货币/设置。

远离Yii的活动记录时:

  1. 您想同时更新多个100条记录。 (太多开销模型)

     的Yii ::应用() - &GT; DB-&GT;命令()
     

    可以让你避免重物和检索简单数组数据。

  2. 您想要做先进的连接和查询涉及多个表。

  3. 任何批处理作业! (例如,检查一个支付表,看看哪些客户逾期他们的付款,更新数据库值等。)

我爱Yii的活动记录,但我交换活动记录模式和普通的SQL之间(使用Yii ::应用程序() - > DB)。基于应用程序的要求

在结束时,我可以选择我是否要更新单个用户的货币

  $用户自&GT;信息 - &GT;货币='美元';
$用户可&GT;信息 - &GT;保存();
 

如果我想更新所有用户货币:

 的Yii ::应用() - &GT; DB-&GT;命令(UPDATE ..... SET货币=美元在哪里......);
 

I recently started working with Yii PHP MVC Framework. I'm looking for advice on how should I continue working with the database through the framework: should I use framework's base class CActiveRecord which deals with the DB, or should I go with the classic SQL query functions (in my case mssql)?

Obviously or not, for me it seems easier to deal with the DB through classic SQL queries, but, at some point, I imagine there has to be an advantage in using framework's way. Some SQL queries will get pretty complex pretty often. I just can't comprehend how the framework could help me and not make things more complicated than they actually are.

解决方案

Very General rule from my experience with Yii and massive databases:

Use Yii Active Record when:

  1. You want to retrieve and post single to a few rows in the database (e.g. user changing his/her settings, updating users balance, adding a vote, getting a count of users online, getting the number of posts under a topic, checking if a model exists)
  2. You want to rapidly design a hierarchical model structure between your tables, (e.g. $user->info->email,$user->settings->currency) allowing you to quickly adjust displayed currency/settings per use.

Stay away from Yii Active Record when:

  1. You want to update several 100 records at a time. (too much overhead for the model)

    Yii::app()->db->command()  
    

    allows you to avoid the heavy objects and retrieves data in simple arrays.

  2. You want to do advanced joins and queries that involve multiple tables.

  3. Any batch job!! (e.g. checking a payments table to see which customers are overdue on their payments, updating database values etc.)

I love Yii Active Record, but I interchange between the Active Record Model and plain SQL (using Yii::app()->db) based on the requirement in the application.

At the end I have the option whether I want to update a single users currency

$user->info->currency = 'USD';
$user->info->save();

or if I want to update all users currencies:

Yii::app()->db->command('UPDATE ..... SET Currency="USD" where ...');

这篇关于使用Yii框架的数据库功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆