前提・実現したいこと
cakephp3を使い、DBのデータをjson形式で出力したい。
発生している問題
ローカルのdocker()では動いていますが、lolipopサーバーではエラーが出ます。
stacktraceはエラー箇所をこの行と指定しています。
$company = $this->Companies->find()->where(['Companies.user_id' => $user_id]);
なので、
'Companies.user_id' => $user_id
が
String ⇒ Integer
となり、エラーが起きるのではないかと思い、
(string)$user_id
としたがダメでした。
エラーメッセージ
Cannot convert value of type string
to integer
InvalidArgumentException
Documentation API
Could this be caused by using Auto-Tables?
Some of the Table objects in your application were created by instantiating "Cake\ORM\Table" instead of any other specific subclass.
This could be the cause for this exception. Auto-Tables are created for you under the following circumstances:
The class for the specified table does not exist.
The Table was created with a typo: $this->getTableLocator()->get('Atricles');
The class file has a typo in the name or incorrect namespace: class Atricles extends Table
The file containing the class has a typo or incorrect casing: Atricles.php
The Table was used using associations but the association has a typo: $this->belongsTo('Atricles')
The table class resides in a Plugin but no plugin notation was used in the association definition.
⟩ Cake\Database\Type\IntegerType->checkNumeric
CORE/src/Database/Type/IntegerType.php, line 117
⟩ Cake\Database\Type\IntegerType->manyToPHP
CORE/src/Database/FieldTypeConverter.php, line 140
⟩ Cake\Database\FieldTypeConverter->__invoke
CORE/src/Database/Statement/CallbackStatement.php, line 58
⟩ Cake\Database\Statement\CallbackStatement->fetch
CORE/src/ORM/ResultSet.php, line 486
⟩ Cake\ORM\ResultSet->_fetchResult
CORE/src/ORM/ResultSet.php, line 276
⟩ Cake\ORM\ResultSet->valid
CORE/src/ORM/ResultSet.php, line 298
⟩ Cake\ORM\ResultSet->first
CORE/src/Datasource/QueryTrait.php, line 472
⟩ Cake\ORM\Query->first
APP/Controller/CompaniesController.php, line 185
⟩ App\Controller\CompaniesController->getForAPI
CORE/src/Controller/Controller.php, line 606
⟩ Cake\Controller\Controller->invokeAction
CORE/src/Http/ActionDispatcher.php, line 120
⟩ Cake\Http\ActionDispatcher->_invoke
CORE/src/Http/ActionDispatcher.php, line 94
⟩ Cake\Http\ActionDispatcher->dispatch
CORE/src/Http/BaseApplication.php, line 256
⟩ Cake\Http\BaseApplication->__invoke
CORE/src/Http/Runner.php, line 65
⟩ Cake\Http\Runner->__invoke
CORE/src/Routing/Middleware/RoutingMiddleware.php, line 164
⟩ Cake\Routing\Middleware\RoutingMiddleware->__invoke
CORE/src/Http/Runner.php, line 65
⟩ Cake\Http\Runner->__invoke
CORE/src/Routing/Middleware/AssetMiddleware.php, line 88
⟩ Cake\Routing\Middleware\AssetMiddleware->__invoke
CORE/src/Http/Runner.php, line 65
⟩ Cake\Http\Runner->__invoke
CORE/src/Error/Middleware/ErrorHandlerMiddleware.php, line 100
⟩ Cake\Error\Middleware\ErrorHandlerMiddleware->__invoke
CORE/src/Http/Runner.php, line 65
⟩ Cake\Http\Runner->__invoke
CORE/src/Http/Runner.php, line 51
⟩ Cake\Http\Runner->run
CORE/src/Http/Server.php, line 97
⟩ Cake\Http\Server->run
ROOT/webroot/index.php, line 40
②ソース
public function getForTest(){
//get param
$user_id = $this->request->getParam('user_id');
//get data
$company = $this->Companies->find()->where(['Companies.user_id' => $user_id]);
if(!empty($company)){
$this->viewClass = 'Json';
$this->set('root', $company);
$this->set('_serialize', array('root'));
return;
}
class Company extends Entity
{
protected $_accessible = [
'companyname' => true,
'postalcode' => true,
'address' => true,
'person' => true,
'tel' => true,
'fax' => true,
'user_id' => true,
'note' => true,
'created' => true,
'modified' => true,
'calls' => true,
];
}
class CompaniesTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('companies'); $this->setDisplayField('id'); $this->setPrimaryKey('id'); $this->addBehavior('Timestamp'); $this->hasMany('Calls', [ 'foreignKey' => 'company_id', ]); $this->belongsTo('Users', [ 'foreignKey' => 'user_id', ]); } public function validationDefault(Validator $validator) { $validator ->integer('id') ->allowEmptyString('id', null, 'create'); $validator ->scalar('companyname') ->maxLength('companyname', 100) ->requirePresence('companyname', 'create') ->notEmptyString('companyname'); $validator ->scalar('postalcode') ->allowEmptyString('postalcode'); $validator ->scalar('address') ->maxLength('address', 200) ->allowEmptyString('address'); $validator ->scalar('person') ->maxLength('person', 50) ->allowEmptyString('person'); $validator ->requirePresence('tel', 'create') ->notEmptyString('tel'); return $validator; } public function buildRules(RulesChecker $rules) { $rules->add($rules->isUnique(['user_id', 'tel'], 'no unique tel.')); $rules->add($rules->existsIn(['user_id'], 'Users')); return $rules; }
}
補足情報(FW/ツールのバージョンなど)
ローカル:Docker, Apache2.4, php7.4, cakephp3.9.10, mysql5.7,
サーバー:lolipop, Apache2.4, php7.4, cakephp3.9.10, mysql5.6,
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/22 14:05