Block building -- edit/update

I’m building a simple block that includes several fields including two file objects (audio & image). Only one of which is required (audio).

When adding the block I leave the image unpicked and it works fine.

However if I add the image and later attempt to remove it I get an SQL error.

SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ‘’ for column ‘audioImage’ at row 1

How do I deal with this in the controller.php file? Assuming that’s the issue. I’m stumped.

TIA.

Greg

If you post your controller code it might give a clue to what is breaking your block.

<?php
namespace Application\Block\AcePlayer;
use Concrete\Core\Block\BlockController;
use Core;
use File;
use Page;
use URL;

class Controller extends BlockController
{
	protected $btTable = 'btAceAudioPlayer';
	protected $btInterfaceWidth = '640';
	protected $btInterfaceHeight = '800';
	protected $btWrapperClass = 'ccm-ui';
	protected $btDefaultSet = 'multimedia';
	protected $pkg = 'ace_player';
	protected $btExportFileColumns = ['fID'];
	
	protected $fID;
	protected $maxWidth;
	protected $maxWidth2;

	public $btFieldsRequired = ['audioTitle', 'audioDescription', 'audioFile', 'audioDownload'];
	protected $btIgnorePageThemeGridFrameworkContainer = false;
	protected $btCacheBlockRecord = true;
	protected $btCacheBlockOutput = true;
	protected $btCacheBlockOutputOnPost = true;

	public function getBlockTypeName()
	{
		return t('ACE Player');
	}
	
	public function getBlockTypeDescription()
	{
		return t('Simple audio player with optional download.');
	}

	public function view()
	{
		// GET MP3 AUDIO FILE
		$audioFile_id = $this->audioFile;
		$f = File::getByID(	$audioFile_id);
		$url = $f->getRelativePath();
		$this->set("audioFile", $url);
		
		// GET IMAGE PATH
		if($audioImage_id = $this->audioImage) {
			$f = File::getByID($audioImage_id);
			$url = $f->getRelativePath();
		} else {
			$url = '';
		}
		$this->set("audioImage", $url);
		
		// AUDIO DOWNLOAD
		$audioDownload_options = [
			'0' => "No",
			'1' => "Yes"
		];
		$this->set("audioDownload_options", $audioDownload_options);
	}

	public function add()
	{
		$this->addEdit();
		$this->set("audioDownload", '1');
	}

	public function edit()
	{
		$this->addEdit();
	}

	protected function addEdit()
	{
		$this->set("audioDownload_options", [
				'0' => "No",
				'1' => "Yes"
			]
		);
	}

	public function save($args)
	{
		parent::save($args);
	}

	public function validate($args)
	{
		$e = Core::make("helper/validation/error");
		if (in_array("audioTitle", $this->btFieldsRequired) && (trim($args["audioTitle"]) == "")) {
			$e->add(t("The %s field is required.", t("Audio Title")));
		}
		if (in_array("audioDescription", $this->btFieldsRequired) && trim($args["audioDescription"]) == "") {
			$e->add(t("The %s field is required.", t("Description")));
		}
		if (in_array("audioFile", $this->btFieldsRequired) && (!isset($args["audioFile"]) || trim($args["audioFile"]) == "" || !is_object(File::getByID($args["audioFile"])))) {
			$e->add(t("The %s field is required.", t("MP3")));
		}
		if ((in_array("audioDownload", $this->btFieldsRequired) && (!isset($args["audioDownload"]) || trim($args["audioDownload"]) == "")) || (isset($args["audioDownload"]) && trim($args["audioDownload"]) != "" && !in_array($args["audioDownload"], ["0", "1"]))) {
			$e->add(t("The %s field has an invalid value.", t("Enable Direct Download")));
		}
		return $e;
	}
	
}

Thanks.

Posted above

Try this controller code:

<?php
namespace Application\Block\AcePlayer;
use Concrete\Core\Block\BlockController;
use Core;
use File;
use Page;
use URL;

class Controller extends BlockController
{
	protected $btTable = 'btAceAudioPlayer';
	protected $btInterfaceWidth = '640';
	protected $btInterfaceHeight = '800';
	protected $btWrapperClass = 'ccm-ui';
	protected $btDefaultSet = 'multimedia';
	protected $pkg = 'ace_player';
	protected $btExportFileColumns = ['fID'];
	
	protected $fID;
	protected $maxWidth;
	protected $maxWidth2;

	public $btFieldsRequired = ['audioTitle', 'audioDescription', 'audioFile', 'audioDownload'];
	protected $btIgnorePageThemeGridFrameworkContainer = false;
	protected $btCacheBlockRecord = true;
	protected $btCacheBlockOutput = true;
	protected $btCacheBlockOutputOnPost = true;

	public function getBlockTypeName()
	{
		return t('ACE Player');
	}
	
	public function getBlockTypeDescription()
	{
		return t('Simple audio player with optional download.');
	}

	public function view()
	{
		// GET MP3 AUDIO FILE
		$audioFile_id = $this->audioFile;
		$f = File::getByID(	$audioFile_id);
		$url = $f->getRelativePath();
		$this->set("audioFile", $url);
		
		// GET IMAGE PATH
		if($audioImage_id = $this->audioImage) {
			$f = File::getByID($audioImage_id);
			$url = $f->getRelativePath();
		} else {
			$url = '';
		}
		$this->set("audioImage", $url);
		
		// AUDIO DOWNLOAD
		$audioDownload_options = [
			'0' => "No",
			'1' => "Yes"
		];
		$this->set("audioDownload_options", $audioDownload_options);
	}

	public function add()
	{
		$this->addEdit();
		$this->set("audioDownload", '1');
	}

	public function edit()
	{
		$this->addEdit();
	}

	protected function addEdit()
	{
		$this->set("audioDownload_options", [
				'0' => "No",
				'1' => "Yes"
			]
		);
	}

public function save($args)
{
    // Optional image: store 0 when no file is selected
    if (empty($args['audioImage'])) {
        $args['audioImage'] = 0;
    } else {
        $args['audioImage'] = (int) $args['audioImage'];
    }

    // Required audio file
    if (isset($args['audioFile'])) {
        $args['audioFile'] = (int) $args['audioFile'];
    }

    parent::save($args);
}

	public function validate($args)
	{
		$e = Core::make("helper/validation/error");
		if (in_array("audioTitle", $this->btFieldsRequired) && (trim($args["audioTitle"]) == "")) {
			$e->add(t("The %s field is required.", t("Audio Title")));
		}
		if (in_array("audioDescription", $this->btFieldsRequired) && trim($args["audioDescription"]) == "") {
			$e->add(t("The %s field is required.", t("Description")));
		}
		if (in_array("audioFile", $this->btFieldsRequired) && (!isset($args["audioFile"]) || trim($args["audioFile"]) == "" || !is_object(File::getByID($args["audioFile"])))) {
			$e->add(t("The %s field is required.", t("MP3")));
		}
		if ((in_array("audioDownload", $this->btFieldsRequired) && (!isset($args["audioDownload"]) || trim($args["audioDownload"]) == "")) || (isset($args["audioDownload"]) && trim($args["audioDownload"]) != "" && !in_array($args["audioDownload"], ["0", "1"]))) {
			$e->add(t("The %s field has an invalid value.", t("Enable Direct Download")));
		}
		return $e;
	}
	
}

Thank you! That’s working.

Mark this as solved.