How to update data from shell (CakePHP 2)


#1

I’m trying to write a shell command to perform house keeping tasks on a database - stuff update time sensitive activities and records etc on a membership data base

I’ve created the follow command

class UpdateMemberShell extends AppShell {
	public function main() {
		App::import('Model', 'Member');
		$this->Member = ClassRegistry::init('Member');

		$memberList = $this->Member->find('all', [
			'fields' => ['id'],
			'order' => ['id' => 'ASC']
		]);
		if (empty($memberList) == false) {
			foreach ($memberList as $member) {
				$this->Member->updateDueAwards($member['Member']['id']);
			}
		}
	}
}

It runs, it loops through the members but the save fails. I’ve thrown in checking code and the save returns false. This is the save that happens after the various logic.

if (empty($member) === false){
	$member['Member']['officer_service_award_due'] = $officerServiceAwardDue;
	$member['Member']['service_award_due'] = $serviceAwardDue;
	$status = $this->save($member, ['callbacks' => true]);
	$this->clear();			
}

Same function updateDueAwards works when run from the related Awards models so its works in the app.

This one has me stumped :frowning:

Thanks in advance for any assistance.

Regards,
Brian


#2

Have you checked for validation errors when the save fails?


#3

Hi,

I’ve tried the following but it just returns an empty array;

		if (!$this->save($member, false)) {
			debug($this->validationErrors);
			die();
		}

###########################
/aal-admin/Model/Member.php (line 1972)
########## DEBUG ##########
array()
###########################

#4

Any chance you have triggers or constraints in your database that are failing? Not sure how you’d log or check such a thing in 2.x.