<commit_before><?php

App::uses('AppModel', 'Model');

class TemplateElement extends AppModel {

	public $actsAs = array('Containable');

	public $hasMany = array(
		'TemplateElementAttribute' => array(
			'dependent' => true
		),
		'TemplateElementText' => array(
			'dependent' => true
		),
		'TemplateElementFile' => array(
			'dependent' => true
		)
	);

	public $belongsTo = array('Template');

	public function lastPosition($template_id) {
		$result = $this->find('first', array(
			'fields' => array('MAX(position) AS pos', 'id', 'template_id'),
			'conditions' => array('template_id' => $template_id),
			'order' => array('id'),
			'group' => array('id')
		));
		return $result[0]['pos'];
	}
}
<commit_msg>fix: Fixed an issue where adding an attribute to an empty temlate as a first element caused an error, fixes #1635<commit_after><?php

App::uses('AppModel', 'Model');

class TemplateElement extends AppModel {

	public $actsAs = array('Containable');

	public $hasMany = array(
		'TemplateElementAttribute' => array(
			'dependent' => true
		),
		'TemplateElementText' => array(
			'dependent' => true
		),
		'TemplateElementFile' => array(
			'dependent' => true
		)
	);

	public $belongsTo = array('Template');

	public function lastPosition($template_id) {
		$result = $this->find('first', array(
			'fields' => array('MAX(position) AS pos', 'id', 'template_id'),
			'conditions' => array('template_id' => $template_id),
			'order' => array('id'),
			'group' => array('id')
		));
		if (empty($result)) {
			return 0;
		}
		return $result[0]['pos'];
	}
}

{'fraction_non_alphanumeric': 0.18546845124282982, 'fraction_numerical': 0.004461440407903123, 'mean_word_length': 4.528169014084507, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5082482', 'n_tokens_mistral': 572, 'n_tokens_neox': 510, 'n_words': 122}