<commit_before><?php

/**
 * @copyright  <NAME>
 * @license    http://www.makoframework.com/license
 */

namespace mako\database\midgard\traits;

use \DateTime;

/**
 * Timestamped trait.
 *
 * @author  <NAME>
 */

trait TimestampedTrait
{
	/**
	 * Returns the column that holds the "created at" timestamp.
	 * 
	 * @access  public
	 * @return  string
	 */

	public function getCreatedAtColumn()
	{
		return isset($this->createdAtColumn) ? $this->createdAtColumn : 'created_at';
	}

	/**
	 * Returns the column that holds the "updated at" timestamp.
	 * 
	 * @access  public
	 * @return  string
	 */

	public function getUpdatedAtColumn()
	{
		return isset($this->updatedAtColumn) ? $this->updatedAtColumn : 'updated_at';
	}

	/**
	 * Returns the date time columns.
	 * 
	 * @access  public
	 * @return  array
	 */

	protected function getDateTimeColumns()
	{
		return array_merge(parent::getDateTimeColumns(), [$this->getCreatedAtColumn(), $this->getUpdatedAtColumn()]);
	}

	/**
	 * Allows you to update the "updated at" timestamp without modifying any data.
	 * 
	 * @access  public
	 * @return  boolean
	 */

	public function touch()
	{
		if($this->exists)
		{
			$this->columns[$this->getUpdatedAtColumn()] = null;

			return $this->save();
		}

		return false;
	}

	/**
	 * Saves the record to the database.
	 * 
	 * @access  public
	 * @return  boolean
	 */

	public function save()
	{
		// Set timestamps

		$dateTime = new DateTime;

		if(!$this->exists)
		{
			$this->columns[$this->getCreatedAtColumn()] = $dateTime;
		}

		if(!$this->exists || $this->isModified())
		{
			$this->columns[$this->getUpdatedAtColumn()] = $dateTime;
		}

		// Save record

		$saved = parent::save();

		// Touch related records

		if($saved === true && !empty($this->touch))
		{
			foreach($this->touch as $touch)
			{
				$relation = $this->$touch();

				$relation->update([$relation->getModel()->getUpdatedAtColumn() => $dateTime]);
			}
		}

		// Return save status

		return $saved;
	}
}<commit_msg>Added support for touching nested relations<commit_after><?php

/**
 * @copyright  <NAME>
 * @license    http://www.makoframework.com/license
 */

namespace mako\database\midgard\traits;

use \DateTime;

/**
 * Timestamped trait.
 *
 * @author  <NAME>
 */

trait TimestampedTrait
{
	/**
	 * Returns the column that holds the "created at" timestamp.
	 * 
	 * @access  public
	 * @return  string
	 */

	public function getCreatedAtColumn()
	{
		return isset($this->createdAtColumn) ? $this->createdAtColumn : 'created_at';
	}

	/**
	 * Returns the column that holds the "updated at" timestamp.
	 * 
	 * @access  public
	 * @return  string
	 */

	public function getUpdatedAtColumn()
	{
		return isset($this->updatedAtColumn) ? $this->updatedAtColumn : 'updated_at';
	}

	/**
	 * Returns the date time columns.
	 * 
	 * @access  public
	 * @return  array
	 */

	protected function getDateTimeColumns()
	{
		return array_merge(parent::getDateTimeColumns(), [$this->getCreatedAtColumn(), $this->getUpdatedAtColumn()]);
	}

	/**
	 * Allows you to update the "updated at" timestamp without modifying any data.
	 * 
	 * @access  public
	 * @return  boolean
	 */

	public function touch()
	{
		if($this->exists)
		{
			$this->columns[$this->getUpdatedAtColumn()] = null;

			return $this->save();
		}

		return false;
	}

	/**
	 * Touches related records.
	 * 
	 * @access  protected
	 * @param   \DateTime  $dateTime  DateTime instance
	 */

	protected function touchRelated($dateTime)
	{
		foreach($this->touch as $touch)
		{
			$touch = explode('.', $touch);

			$relation = $this->{array_shift($touch)}();

			foreach($touch as $nested)
			{
				$related = $relation->first();

				if($related === false)
				{
					continue 2;
				}

				$relation = $related->$nested();
			}

			$relation->update([$relation->getModel()->getUpdatedAtColumn() => $dateTime]);
		}
	}

	/**
	 * Saves the record to the database.
	 * 
	 * @access  public
	 * @return  boolean
	 */

	public function save()
	{
		// Set timestamps

		$dateTime = new DateTime;

		if(!$this->exists)
		{
			$this->columns[$this->getCreatedAtColumn()] = $dateTime;
		}

		if(!$this->exists || $this->isModified())
		{
			$this->columns[$this->getUpdatedAtColumn()] = $dateTime;
		}

		// Save record

		$saved = parent::save();

		// Touch related records

		if($saved === true && !empty($this->touch))
		{
			$this->touchRelated($dateTime);
		}

		// Return save status

		return $saved;
	}
}
{'fraction_non_alphanumeric': 0.16160252081926627, 'fraction_numerical': 0.00022507314877335134, 'mean_word_length': 3.118628359592215, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4436809', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1418, 'n_words': 321}