LoginSignup
1
1

More than 5 years have passed since last update.

Mailer でloadModel を利用できるようにする (CakePHP3)

Last updated at Posted at 2016-04-15

Mailer でモデルを使いたい

CakePHP3には Resuable-email という仕組みがあり、Mailer クラスを利用する事で、メール送信に関連する処理を別クラスに切り離せます。

apiドキュメントを見ると、ModelAwareTraitを指定されており、モデルも利用可能なのかと思いModelを利用し使ってみると、

InvalidArgumentException

Unknown repository type "Table". Make sure you register a type before trying to use it.

怒られた。modelのインスタンスを生成する手段が登録されてないという意味なので、
以下で対応

namespace App\Mailer;

use Cake\Mailer\Mailer;

class UserMailer extends Mailer
{

    public function __construct(\Cake\Mailer\Email $email = null)
    {
        parent::__construct($email);

        /**
         * loadModelを利用可能にする
         * modelFactory にTableRegistory::get を指定。
         */
        $this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']); 

        $this->loadModel('Users');
    }



    public function notify($data)
    {
        $users = $this->Users->find('target');
        .....
    }
}

でModelを利用可能となった。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1