<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://atk-framework.com/index.php?action=history&amp;feed=atom&amp;title=Using_descriptors</id>
	<title>Using descriptors - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://atk-framework.com/index.php?action=history&amp;feed=atom&amp;title=Using_descriptors"/>
	<link rel="alternate" type="text/html" href="https://atk-framework.com/index.php?title=Using_descriptors&amp;action=history"/>
	<updated>2026-09-20T17:19:39Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://atk-framework.com/index.php?title=Using_descriptors&amp;diff=3701&amp;oldid=prev</id>
		<title>bahtiar: Using custom descriptors moved to Using descriptors</title>
		<link rel="alternate" type="text/html" href="https://atk-framework.com/index.php?title=Using_descriptors&amp;diff=3701&amp;oldid=prev"/>
		<updated>2007-08-16T16:15:10Z</updated>

		<summary type="html">&lt;p&gt;Using custom descriptors moved to Using descriptors&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 16:15, 16 August 2007&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>bahtiar</name></author>
	</entry>
	<entry>
		<id>https://atk-framework.com/index.php?title=Using_descriptors&amp;diff=3691&amp;oldid=prev</id>
		<title>bahtiar at 16:15, 16 August 2007</title>
		<link rel="alternate" type="text/html" href="https://atk-framework.com/index.php?title=Using_descriptors&amp;diff=3691&amp;oldid=prev"/>
		<updated>2007-08-16T16:15:01Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{howto|Easy|[[Ivo Jansch]] &amp;lt;ivo@achievo.org&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== What is a descriptor? ==&lt;br /&gt;
&lt;br /&gt;
A descriptor is a piece of text that identifies a record to a user. For example, say you have a table with products. Each product has a unique id. The &amp;#039;id&amp;#039; field is what we call the &amp;#039;identifier&amp;#039; or the &amp;#039;key&amp;#039;. This is how the database recognizes a record.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Say you have the following products in your database:&lt;br /&gt;
&lt;br /&gt;
  ID      NAME&lt;br /&gt;
  -------------------------&lt;br /&gt;
  102     Nintendo Wii&lt;br /&gt;
  294     Apple MacBook Pro&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The database identifies these records by their id. So if you want to delete the Nintendo Wii (hypothetically speaking; there&amp;#039;s no reason to delete a Wii of course), you&amp;#039;d say &amp;#039;DELETE FROM products WHERE id=102&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Humans don&amp;#039;t like id&amp;#039;s though. If in your application you need to display a dropdown of products, the numbers &amp;#039;102&amp;#039; and &amp;#039;294&amp;#039; wouldn&amp;#039;t mean anything to the average user.&lt;br /&gt;
&lt;br /&gt;
Humans would identify the product by their name. So we call the &amp;#039;name&amp;#039; field the &amp;#039;descriptor&amp;#039; of the record.&lt;br /&gt;
&lt;br /&gt;
If we tell ATK what the descriptor of a certain node is, ATK will take care of displaying it to the user whenever a record is displayed on screen.&lt;br /&gt;
&lt;br /&gt;
== What is a descriptor template? ==&lt;br /&gt;
&lt;br /&gt;
Sometimes a descriptor consists of a single field, as in the example above. But there may be situations where this is not enough information for the user to distinguish the record. Take the following example:&lt;br /&gt;
&lt;br /&gt;
  ID      PRODUCT            FLAVOUR&lt;br /&gt;
  -------------------------------------------&lt;br /&gt;
  913     Milkshake          Banana&lt;br /&gt;
  213     Milkshake          Strawberry&lt;br /&gt;
&lt;br /&gt;
The product name in this case is not unique enough, nor is the flavour. A good descriptor in this case would be: &amp;quot;Milkshake (Banana flavour)&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
We can tell ATK to create descriptors for records this way using a &amp;#039;descriptor template&amp;#039;. This is similar to an HTML template, and basically consists of a string with a few fieldnames in them.&lt;br /&gt;
&lt;br /&gt;
The descriptor template for our milkshake example would be:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[product] ([flavour] flavour)&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The [Product] and [flavour] fields will be replaced at runtime by their actual values, depending on which record the system is rendering.&lt;br /&gt;
&lt;br /&gt;
== How do I set the descriptor template? ==&lt;br /&gt;
&lt;br /&gt;
=== The canonical way ===&lt;br /&gt;
&lt;br /&gt;
The easiest way is to create a small method inside your node that provides the system the descriptor template. This is a method that is from a distant past, so its name does not adhere to modern naming conventions, but here is an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function descriptor_def()&lt;br /&gt;
{&lt;br /&gt;
  return &amp;quot;[product] ([flavour] flavour)&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever ATK needs to render a descriptor for your node, it will call this function to find out what the template is.&lt;br /&gt;
&lt;br /&gt;
== The setDescriptorTemplate way ==&lt;br /&gt;
&lt;br /&gt;
You can call this in the constructor of your node (or anywhere else, but usually the constructor is suitable):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
  $this-&amp;gt;setDescriptorTemplate(&amp;quot;[product] ([flavour] flavour)&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== In a relationship ==&lt;br /&gt;
&lt;br /&gt;
If some node has a relationship with a different node but it doesn&amp;#039;t like the descriptor that is set in the related node, it can change it on the fly:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
  $rel = &amp;amp;$this-&amp;gt;add(new atkManyToOneRelation(&amp;quot;product&amp;quot;, &amp;quot;shop.product&amp;quot;));&lt;br /&gt;
  $rel-&amp;gt;setDescriptorTemplate(&amp;quot;[flavour] [product]&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Dynamic descriptors = &lt;br /&gt;
&lt;br /&gt;
Sometimes you will want descriptors to not be just a template, but to depend on the values of a record.&lt;br /&gt;
&lt;br /&gt;
There are 2 ways to do this.&lt;br /&gt;
&lt;br /&gt;
== The canonical way ==&lt;br /&gt;
&lt;br /&gt;
Instead of a descriptor_def method, implement a combination of descriptorFields() and descriptor(). It&amp;#039;s difficult to describe their purpose without an example, so take the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
  function descriptor($record)&lt;br /&gt;
  {&lt;br /&gt;
     if ($record[&amp;quot;type&amp;quot;]==&amp;quot;food&amp;quot;) return $record[&amp;quot;name&amp;quot;].&amp;quot; - &amp;quot;.$record[&amp;quot;calories&amp;quot;].&amp;quot; calories&amp;quot;;&lt;br /&gt;
     else return $record[&amp;quot;name&amp;quot;];&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a descriptor on the fly, based on the values of the $record that is being rendered.&lt;br /&gt;
&lt;br /&gt;
ATK has some optimisations that make sure that data is only loaded if necessary. ATK doesn&amp;#039;t know that you&amp;#039;ll be needing the name and calories fields in this function, so they won&amp;#039;t be present by default in $record.&lt;br /&gt;
&lt;br /&gt;
This is why you need the second function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
  function descriptorFields()&lt;br /&gt;
  {&lt;br /&gt;
    return array(&amp;quot;name&amp;quot;, &amp;quot;type&amp;quot;, &amp;quot;calories&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
ATK will call this function prior to calling your descriptor. It will now know that you&amp;#039;ll be needing the name, type and calories fields, and will make sure that $record is populated accordingly.&lt;br /&gt;
&lt;br /&gt;
== Using a handler ==&lt;br /&gt;
&lt;br /&gt;
It is also possible to place the descriptor method in an object, so you can reuse it&amp;#039;s logic:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$this-&amp;gt;setDescriptorHandler(new myCustomDescriptor());&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you&amp;#039;d implement this class like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
class myCustomDescriptor&lt;br /&gt;
{&lt;br /&gt;
  function descriptor($record)&lt;br /&gt;
  {&lt;br /&gt;
    return $record[&amp;quot;name&amp;quot;];&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>bahtiar</name></author>
	</entry>
</feed>