Код: Выделить всё
namespace App\Domain\Customer\Data;
use Cake\Database\Query;
final class CustomerFinderResult
{
/**
* @var CustomerFinderItem[]
*/
public array $customers = [];
private Query $query;
public function __construct(Query $query)
{
$this->query = $query;
}
public function updateSubscriptionStatus(string $subscriptionId, string $status): void
{
$query = $this->query->update('customers')
->set(['subscription_status' => $status])
->where(['paypal_subscription_id' => $subscriptionId]);
$query->execute();
}
Я обратился к Gemini за помощью, но запрос, который он мне дал, не удался, потому что используемый им метод не существует, говорится в полученном мной сообщении об ошибке.
Код: Выделить всё
public function updateSubscriptionStatus(string $subscriptionId, string $status): void
{
$customer = $this->Customers->find()->where(['paypal_subscription_id' =>
$subscriptionId])->firstOrFail();
$customer->subscription_status = $status;
$this->Customers->save($customer);
}
Код: Выделить всё
$query->update('articles')->set(['title' => 'The Title'], ['title' => 'string']);
Код: Выделить всё
$query = $this->query->update('customers')
->set(
['subscription_status' => $status],
['paypal_subscription_id' => $subscriptionId]
);
$query->execute();
В классе Query для set. вот это.
Код: Выделить всё
/**
* Set one or many fields to update.
*
* ### Examples
*
* Passing a string:
*
* ```
* $query->update('articles')->set('title', 'The Title');
* ```
*
* Passing an array:
*
* ```
* $query->update('articles')->set(['title' => 'The Title'], ['title' => 'string']);
* ```
*
* Passing a callable:
*
* ```
* $query->update('articles')->set(function ($exp) {
* return $exp->eq('title', 'The title', 'string');
* });
* ```
*
* @param \Cake\Database\Expression\QueryExpression|\Closure|array|string $key The column name or array of keys
* + values to set. This can also be a QueryExpression containing a SQL fragment.
* It can also be a Closure, that is required to return an expression object.
* @param mixed $value The value to update $key to. Can be null if $key is an
* array or QueryExpression. When $key is an array, this parameter will be
* used as $types instead.
* @param array|string $types The column types to treat data as.
* @return $this
*/
public function set($key, $value = null, $types = [])
{
if (empty($this->_parts['set'])) {
$this->_parts['set'] = $this->newExpr()->setConjunction(',');
}
if ($key instanceof Closure) {
$exp = $this->newExpr()->setConjunction(',');
$this->_parts['set']->add($key($exp));
return $this;
}
if (is_array($key) || $key instanceof ExpressionInterface) {
$types = (array)$value;
$this->_parts['set']->add($key, $types);
return $this;
}
if (!is_string($types)) {
$types = null;
}
$this->_parts['set']->eq($key, $value, $types);
return $this;
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... nd-mariadb
Мобильная версия