标签: cakephp

CakePHP - 在非对象错误时调用成员函数allow()

我正在 CakePHP 中创建一个登录身份验证应用程序,并收到此致命错误:在第 5 行的 /var/www/cakephp1/app/Controller/users_controller.php 中的非对象上调用成员函数allow()

这是我的控制器代码 users_controller.php

<?php
class UsersController extends AppController {
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add');
    }
    public function add() {
        if (!empty($this->data)) {
            $this->User->create();
            if ($this->User->save($this->data)) {
                $this->Session->setFlash('User created!');
                $this->redirect(array('action'=>'login'));
            } else {
                $this->Session->setFlash('Please correct the errors');
            }
        }
        $this->set('groups', $this->User->Group->find('list'));
    }
    public function login() {
    }
    public function logout() {
        $this->redirect($this->Auth->logout());
    }
    public function dashboard() {
        $groupName = $this->User->Group->field('name', 
            array('Group.id'=>$this->Auth->user('group_id'))
        );
        $this->redirect(array('action'=>strtolower($groupName)));
    }
    public function user() {
    }
    public function administrator() {
    } …
Run Code Online (Sandbox Code Playgroud)

php cakephp cakephp-1.3

0
推荐指数
1
解决办法
2万
查看次数

cakephp 和多个复选框

我不会对 habtm 做任何花哨的事情。

我想做的是在 cakephp 中有多个复选框,然后使用它们。

所以我认为有这段代码:

<div class="deleteButton">
    <?php echo $form->input('', array('type'=>'checkbox',
    'value'=>$video['Video']['id'], 'label'=>false, 'hiddenField'=>false,
    'id'=>false, 'multiple'=>true)); ?>
</div>
Run Code Online (Sandbox Code Playgroud)

这在渲染页面中给出了这个 html:

<input type="checkbox" value="30" name="data[Videos]">
<input type="checkbox" value="31" name="data[Videos]">
<input type="checkbox" value="32" name="data[Videos]">
Run Code Online (Sandbox Code Playgroud)

现在,当我在 html 中选择它并提交时,我在控制器中只得到一个值: ... pr($this->data); ...

看起来 cakephp 正在以某种方式覆盖存储的值,因为我得到的 ONE 值始终是最后选中的复选框值。

我究竟做错了什么?

更新:天啊我讨厌cakephp

使用这个:http ://nuts-and-bolts-of-cakephp.com/2008/05/05/multiple-checkboxes/

或者更确切地说,这个:: input($video['Video']['id'], array('type'=>'checkbox', 'value'=>$video['Video']['id'], '标签'=>假,'隐藏字段'=>假,'多个'=>真));?>

(我将 $video['Video']['id'] 作为第一个条目,显然是用于 ids 的)

我的请求现在被 cakephp 安全组件中完全可笑的黑洞所屏蔽。老实说,为什么 404 页面没有一个可切换的错误?现在我必须弄清楚如何从中得到有意义的错误......

欢迎任何想法!

更新:已解决:嗯,相反我需要:

<?php echo $form->input("Video".$video['Video']['id']."id", array('type'=>'checkbox', 'value'=>$video['Video']['id'], 'label'=>false, 'hiddenField'=>false, 'multiple'=>true)); ?> 
Run Code Online (Sandbox Code Playgroud)

也就是说,我用“Video”和“id”包装了 $video['Video']['id'] 。

NFI …

security checkbox cakephp

0
推荐指数
1
解决办法
2800
查看次数

如何在 cakephp 中做 patchEntity

嗨,我正在制作一个网页以从注册者那里获取电话号码,并将随机数作为密码保存在数据库中。我想显示注册者输入随机数。

所以我做了这些代码。

class RegistersController extends AppController{

    public function index()
    {
        $random = rand(11111,99999);
        $register = $this->Registers->newEntity($this->request->data);
        if($this->Registers->save($register)) {
            $this->Flash->success('The Phone number has been sent.');
            $reg = $this->Registers->patchEntity($random);
            $this->Registers->save($reg);
            return $this->redirect(['action' => 'certnum']);
        }
        $this->set(compact('register'));
    }
}
Run Code Online (Sandbox Code Playgroud)

但不知何故,它产生了致命的错误,我不知道该怎么办?

我是否必须将随机数放入具有特定 id 号或其他内容的数组中?

请帮忙。

谢谢

merge cakephp save cakephp-3.0

0
推荐指数
1
解决办法
9575
查看次数

解除CakePHP中所有模型的绑定

CakePHP 中是否有对unbind之前所有关联模型的函数find()

$this->Model1->unbindModel(array('hasMany'=>array('Model2'),'belongsTo'=>array('Model3')));
Run Code Online (Sandbox Code Playgroud)

我只想从中得到结果Model1。但由于我有很多型号,所以我不想像unbindModel这样单独使用。

cakephp cakephp-2.0

0
推荐指数
1
解决办法
2424
查看次数

如何检查该元素是否存在于 Cakephp 3 中?

检查 cakephp 3 中是否存在一个元素,一个 .ctp 文件的最佳方法是什么?

像这样的东西,

<?php
// if the elemet exist display it
$this->render('/Element/Trip/'.$current_step);
// else display another element instead
?>
Run Code Online (Sandbox Code Playgroud)

谢谢 :)

php cakephp

0
推荐指数
1
解决办法
1535
查看次数

如何只选择那些至少有一个关联记录的记录?

我有以下关联:OrdersbelongsToSites

我执行以下查询:

$sites = $this->Sites->find('all')
->select(['id', 'name'])
->contain([
        'Users' => function ($q) {
                return $q->select(['id', 'owner_id', 'firstname', 'lastname']);
        },
        'Orders' => function ($q) {
                return $q->where(['status >' => 0]);
        },
        'Orders.Agplans'
])
->matching('Users', function ($q) use($owner_id)
{
    return $q->where([
        'Users.owner_id' => $owner_id
    ]);
})
->all();
Run Code Online (Sandbox Code Playgroud)

但大多数网站都没有订单,所以我得到了一些结果:

(int) 100 => object(App\Model\Entity\Site) {
    'id' => (int) 7966,
    'name' => 'Site example',
    'orders' => [],
    'users' => [
        (int) 0 => object(App\Model\Entity\User) {

    ...
Run Code Online (Sandbox Code Playgroud)

是否可以在查询中指定我只想sites不为空orders

cakephp query-builder cakephp-3.1

0
推荐指数
1
解决办法
749
查看次数

Cakephp 3 验证错误显示

我试图为表单中的每个错误输入一起显示错误消息。

例如:用户必须设置他的姓名、年龄和电子邮件,但他只设置了姓名。验证器在年龄字段的 notEmpty 规则上正确返回 false。但它总是返回一个错误,但我需要得到两个错误 - 第一个错误为空年龄字段,第二个错误为空电子邮件字段。

用户添加年龄并提交数据后,(邮件字段仍为空)验证器在电子邮件的 notEmpty 规则上返回错误。但我需要这个错误以及之前提交中的年龄错误。

如何将错误消息分组在一起?

cakephp cakephp-3.0

0
推荐指数
1
解决办法
1万
查看次数

如何与“null”(空)字段进行比较?

我在 CakePHP 3 应用程序中有一个表,downloads其中有一个名为master. 字段类型设置为TINYINT(1)

我可以找到任何这样的记录downloads.master == 1

$query = $this->Downloads->find()->where(['master' => true]);
Run Code Online (Sandbox Code Playgroud)

但是 Cake 不会让我查询 where downloads.master !== 1。这些都不起作用,并且在执行查询时都返回一个空数组/对象:

$query = $this->Downloads->find()->where(['master' => false]);
$query = $this->Downloads->find()->where(['master' => 0]);
$query = $this->Downloads->find()->where(['master' => null]);
$query = $this->Downloads->find()->where(['master' => '']);
Run Code Online (Sandbox Code Playgroud)

你用什么作为条件来使这成为可能?我的想法是应该是false因为这true与 .

我已经使用 phpMyAdmin 检查了我的表中的记录,master == 1并且确实有两个记录在哪里,master == null所以它不是要返回零结果的情况。

php cakephp query-builder cakephp-3.x

0
推荐指数
1
解决办法
848
查看次数

你如何在 CakePHP 3.x 中发送 JSON 响应

我想生成一个 JSON 响应。我在我的控制器方法中尝试了以下方法:

public function removeFilter($id = null)
{
    $this->autoRender = false;

    header('Content-Type: application/json');
    echo json_encode(['result' => 'filter_removed']);
}
Run Code Online (Sandbox Code Playgroud)

然后按照CakePHP3.4 中的说明进行操作:如何发送 json 对象响应?我也试过:

public function removeFilter($id = null)
{
    $this->autoRender = false;

    return $this->response
    ->withType('application/json')
    ->withStringBody(['result' => 'filter_removed']);
}
Run Code Online (Sandbox Code Playgroud)

这两个都给出了Content-Type: text/html; charset=UTF-8. 没有与此 Controller 方法关联的模板,这就是为什么autoRender = false.

这里出了什么问题?

蛋糕PHP 3.5.13

php cakephp cakephp-3.0

0
推荐指数
1
解决办法
1923
查看次数

CakePHP:文件上传时无法将类 Laminas\Dictoros\UploadedFile 的对象转换为字符串

我正在尝试在 cakephp 版本 4 中上传文件。

我正在关注这个文档

我在控制器中尝试过

if ($this->request->is('post')) {

            $image = $this->request->getData('image');
            $fileName = $image->getClientFilename();

            $targetPath = WWW_ROOT.'img'.DS.$fileName;

            $image->moveTo($targetPath);

            $user = $this->Users->patchEntity($user, $this->request->getData());   //line 58

            $user->image = $fileName;

            $this->Users->save($user);
}
Run Code Online (Sandbox Code Playgroud)

图片上传工作正常,名称也保存在数据库中。但是当发生验证错误时,我得到

Warning (4096): Object of class Laminas\Diactoros\UploadedFile could not be converted to string [CORE\src\Database\Type\StringType.php, line 97]
Run Code Online (Sandbox Code Playgroud)

日志

Cake\Database\Type\StringType::marshal() - CORE\src\Database\Type\StringType.php, line 97
Cake\ORM\Marshaller::Cake\ORM\{closure}() - CORE\src\ORM\Marshaller.php, line 78
Cake\ORM\Marshaller::merge() - CORE\src\ORM\Marshaller.php, line 558
Cake\ORM\Table::patchEntity() - CORE\src\ORM\Table.php, line 2761
App\Controller\UsersController::add() - APP/Controller\UsersController.php, line 58
Cake\Controller\Controller::invokeAction() - CORE\src\Controller\Controller.php, line 524
Cake\Controller\ControllerFactory::invoke() …
Run Code Online (Sandbox Code Playgroud)

cakephp cakephp-4.x

0
推荐指数
1
解决办法
2877
查看次数