感觉phalcon的这种写法好low呀

    $phql = "INSERT INTO Robots (name, type, year) VALUES (:name:, :type:, :year:)";
    $status = $app->modelsManager->executeQuery(
        $phql,
        [
            "name" => $robot->name,
            "type" => $robot->type,
            "year" => $robot->year,
        ]
    );
    
    
    
    感觉这种示例好low呀,有没有简单实用一点的CURD操作???
阅读 4.9k
4 个回答

插入记录:

    public function insert_record(array $data)
    {
        if (count($data) == 0) {
            throw new \Exception('参数错误');
        }
        $result = $this->create($data);
        if (!$result) {
            throw new \Exception(implode(',', $this->getMessages()));
        }
        $id = $this->id;
        return $id;
    }

分页查询:

    public function get_list($page, $pagesize = 10, array $ext = array())
    {
        $page = intval($page);
        $page <= 0 && $page = 1;
        $pagesize = intval($pagesize);
        ($pagesize <= 0 || $pagesize > 20) && $pagesize = 10;

        $builder = $this->getModelsManager()->createBuilder();
        $builder->from(__CLASS__);
        $builder->where('status = :status:', array(
            'status' => 1
        ));

        $paginator = new PaginatorQueryBuilder(array(
            'builder' => $builder,
            'limit' => $pagesize,
            'page' => $page,
        ));
        $result = $paginator->getPaginate();
        return $result;
    }

查询:

    public function detail($mid, array $ext = array())
    {
        $mid = intval($mid);
        if ($mid <= 0) {
            throw new \Exception('参数错误');
        }
        $params = array(
            'conditions' => 'mid = :mid: AND status = :status:',
            'bind' => array(
                'mid' => $mid,
                'status' => 1
            ),
        );
        $result = $this->findFirst($params);
        if (!$result) {
            throw new \Exception('error');
        }
        return $result;
    }

就这么简单 :)

保存直接save();

phalcon CURD操作的方法很多啊。
详情参考phalcon官方文档:
中文:http://phalcon.ipanta.com/2.0

比如添加(控制器层,model层的话就直接self::create(数组)):1、数据的数组,key和数据库字段一致 2、实例化Model进行create
图片描述

比如修改(model层):1、查出你要修改的这条记录,然后save
图片描述

比如删除(model层):和修改一样 查出来直接delete
图片描述

谢谢 @i牛牛丶 @marser @mustang 的回答。

难道在模型层(Model) 和控制层(Controller)使用的方法不一样???我看你们的示例都不一样。所以问一下。

我是phalcon小白,从其它框架转过来的。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏