| 除了可以 映射关系数据库的表 之外,Phalcon还可以使用NoSQL数据库如MongoDB等。Phalcon中的ODM具有可以非常容易的实现如下功能:CRUD,事件,验证等。 因为NoSQL数据库中无sql查询及计划等操作故可以提高数据操作的性能。再者,由于无SQL语句创建的操作故可以减少SQL注入的危险。 当前Phalcon中支持的NosSQL数据库如下:
| 名称 |
描述 |
| MongoDB |
MongoDB是一个稳定的高性能的开源的NoSQL数据 |
创建模型(Creating Models)? NoSQL中的模型类扩展自 Phalcon\Mvc\Collection.模型必须要放入模型文件夹中而且每个模型文件必须只能有一个模型类; 模型类名应该为小驼峰法书写:
class Robots extends \Phalcon\Mvc\Collection { } 如果PHP版本为5.4/5.5或更高版本,为了提高性能节省内存开销,最好在模型类文件中定义每个字段。 模型Robots默认和数据库中的robots表格映射。如果想使用别的名字映射数据库中的表格则只需要重写getSource()方法即可:
class Robots extends \Phalcon\Mvc\Collection { public function getSource() { return "the_robots"; } } 理解文档对象(Understanding Documents To Objects)? 每个模型的实例和数据库表中的一个文档(记录)相对应。我们可以非常容易的通过读取对象属性来访问表格的数据。例如访问robots表格: $ mongo test MongoDB shell version: 1.8.2 connecting to: test > db.robots.find() { "_id" : ObjectId("508735512d42b8c3d15ec4e1"), "name" : "Astro Boy", "year" : 1952, "type" : "mechanical" } { "_id" : ObjectId("5087358f2d42b8c3d15ec4e2"), "name" : "Bender", "year" : 1999, "type" : "mechanical" } { "_id" : ObjectId("508735d32d42b8c3d15ec4e3"), "name" : "Wall-E", "year" : 2008 } > 模型中使用命名空间(Models in Namespaces)? 我们在这里可以使用命名空间来避免类名冲突。这个例子中我们使用getSource方法来标明要使用的数据库表:
namespace Store\Toys; class Robots extends \Phalcon\Mvc\Collection { public function getSource() { return "robots"; } } 我们可以通过对象的ID查找到对象然后打印出其名字:
// Find record with _id = "5087358f2d42b8c3d15ec4e2" $robot = Robots::findById("5087358f2d42b8c3d15ec4e2"); // Prints "Bender" echo $robot->name; 一旦记录被加载到内存中,我们就可以对这些数据进行修改了,修改之后还可以保存:
$robot = Robots::findFirst(array( array('name' => 'Astroy Boy') )); $robot->name = "Voltron"; $robot->save(); 设置连接(Setting a Connection)? 这里的MongoDB服务是从服务容器中取得的。默认,Phalcon会使mongo作服务名:
// Simple database connection to localhost $di->set('mongo', function() { $mongo = new Mongo(); return $mongo->selectDb("store"); }, true); // Connecting to a domain socket, falling back to localhost connection $di->set('mongo', function() { $mongo = new Mongo("mongodb:///tmp/mongodb-27017.sock,localhost:27017"); return $mongo->selectDb("store"); }, true); 查找文档(Finding Documents)? Phalcon\Mvc\Collection
// How many robots are there? $robots = Robots::find(); echo "There are ", count($robots), "\n"; // How many mechanical robots are there? $robots = Robots::find(array( array("type" => "mechanical") )); echo "There are ", count($robots), "\n"; // Get and print mechanical robots ordered by name upward $robots = Robots::find(array( array("type" => "mechanical"), "sort" => array("name" => 1) )); foreach ($robots as $robot) { echo $robot->name, "\n"; } // Get first 100 mechanical robots ordered by name $robots = Robots::find(array( array("type" => "mechanical"), "sort" => array("name" => 1), "limit" => 100 )); foreach ($robots as $robot) { echo $robot->name, "\n"; } 这里我们可以使用findFirst()来取得配置查询的第一条记录:
// What's the first robot in robots collection? $robot = Robots::findFirst(); echo "The robot name is ", $robot->name, "\n"; // What's the first mechanical robot in robots collection? $robot = Robots::findFirst(array( array("type" => "mechanical") )); echo "The first mechanical robot name is ", $robot->name, "\n"; find()和findFirst方法都接收一个关联数据组为查询的条件:
// First robot where type = "mechanical" and year = "1999" $robot = Robots::findFirst(array( "conditions" => array( "type" => "mechanical", "year" => "1999" ) )); // All virtual |