Answers.Body: <p>Craft is built on Yii, and comes with a <a href="http://buildwithcraft.com/docs/plugins/database" rel="nofollow">query builder</a> for raw database access. It's based on <a href="http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder" rel="nofollow">Yii's Query Builder</a> with some added functions.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T03:26:29.493Z
Answers.Text: I can see that but before I create a database query I need to be pointed in the right direction of where i should create and execute it.
Answers.UserDisplayName: user3309362
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T03:01:43.020Z
Answers.LastActivityDate: 2014-06-19T03:01:43.020Z
Answers.UserDisplayName: Bryan Redeagle
Answers.Body: <p>You are correct in stating that Records typically define a new custom table for your plugin. However, don't forget that Craft already comes with many pre-existing Records.</p>
  
  <p>Take a look inside this folder:</p>
  
  <pre><code>/craft/app/records
  </code></pre>
  
  <p>Although based on your question (and the ensuing comments thread), it looks like you may not need to tap directly into any Records at all. I'd recommend you peruse the built-in API to see what may help you get the job done.</p>
  
  <p>Specifically, take a look at the EntriesService API... </p>
  
  <blockquote>
    <p><a href="http://buildwithcraft.com/docs/apis/entriesservice">http://buildwithcraft.com/docs/apis/entriesservice</a></p>
  </blockquote>
  
  <p>I suspect that everything you need to do can be done using those methods. After all, they already allow you to get, modify, and delete entry data.</p>
  
  <p>An important thing to note when dealing with the EntriesService API... You're not directly accessing Entry Records. Instead, you'll be dealing with Entry <strong>Models</strong>. This is greatly preferred, and you'll find that it's much easier to do what you want with a Model. (Behind the scenes, your model will tie into an Entry Record as needed.)</p>
  
  <hr>
  
  <p>On a separate note, it looks like you'll need to take advantage of <strong><a href="http://buildwithcraft.com/docs/plugins/hooks-and-events#events">Events</a></strong>. You mentioned in a comment that you'd like an action to take place "when an entry is saved". To do that, you'll definitely want to take advantage of the <strong><a href="http://buildwithcraft.com/docs/plugins/events-reference#entries-onSaveEntry">entries.onSaveEntry</a></strong> event.</p>
  
  <hr>
  
  <p>In summary, I don't think that you will need to create a custom Record, nor build your own custom Yii query.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T07:40:45.270Z
Answers.LastActivityDate: 2014-06-19T07:40:45.270Z
Answers.UserDisplayName: Lindsey D
Body: <p>I am trying to develop a plugin and what I want to do in this plugin is to be able to access the contents table that is already defined in the database and get and update entries in this table. What would be the best practice to do this? I know records can create new tables but how can I access or manipulate an already created table that is not part of my plugin but is created in the database by default?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-19T02:55:15.710Z
Comments.Text: Why would you need to edit the raw data, and not manipulate it via Services, Records, and Models?
Comments.UserDisplayName: Bryan Redeagle
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-19T03:25:31.013Z
Comments.Text: Yeah I would use Services, Records and models but I do not have an idea how to use a record with a table outside of my own plugin. I mean is there a way to associate a record with the default contents table in the craft database?
Comments.UserDisplayName: user3309362
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-19T04:14:33.573Z
Comments.Text: I would need more information about your plugin and what you wish to accomplish with it before I can give the most accurate answer.
Comments.UserDisplayName: Bryan Redeagle
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-19T05:58:13.223Z
Comments.Text: Ok so basically what i want to do is when i save an entry it should fire up a method that based on the value of the content ( a specific field "Field1" ) of the saved entry ("Entry1") finds another entry ("Entry2") with another field ("Field2") whose value is same as Field1 and modifies the value of another field ("Field3") in Entry2 to reflect the value of Entry1's id.
Comments.UserDisplayName: user3309362
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-19T06:05:00.510Z
Comments.Text: So as an example lets say I have two sections Books and Colors. Also each entry of Colors has a field of bookId, and each entry of Books has a field colorId. I save an entry of Colors with bookId 24 and it should automatically add the this newly saved color entry's id in the Book entry with id 24 (in the colorId field).
Comments.UserDisplayName: user3309362
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-19T07:18:34.880Z
Comments.Text: Not being critical, just curious... What is the purpose behind each section (Books & Colors) having a reference to each others' ids? As in, why does Books have a colorId field **and** Colors has a bookId field? This isn't traditional database architecture... If it is a one-to-many relationship, then only one section needs to reference the other. If it is a many-to-many relationship, then the connecting data will be stored in the **craft_relations** table.
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-19T08:26:24.280Z
Comments.Text: I know it is not a traditional scheme but its like the person I am making this for wants to have a field in the color channel that he chooses himself to associate with books but he wants a field in the books channel to be automatically associated with the color channel.
Comments.UserDisplayName: user3309362
Tags: <plugin-development><database><entrymodel>
Title: Using a plugin, how can I manipulate entry data?
ThreadText: # Using a plugin, how can I manipulate entry data?
  
  (Asked by: username_0 on 2014-06-19)
  
  I am trying to develop a plugin and what I want to do in this plugin is to be able to access the contents table that is already defined in the database and get and update entries in this table. What would be the best practice to do this? I know records can create new tables but how can I access or manipulate an already created table that is not part of my plugin but is created in the database by default?
  
  **Tags:** plugin-development, database, entrymodel
  
  > **Comments:**
  > **username_1**: Why would you need to edit the raw data, and not manipulate it via Services, Records, and Models?  
  > **username_0**: Yeah I would use Services, Records and models but I do not have an idea how to use a record with a table outside of my own plugin. I mean is there a way to associate a record with the default contents table in the craft database?  
  > **username_1**: I would need more information about your plugin and what you wish to accomplish with it before I can give the most accurate answer.  
  > **username_0**: Ok so basically what i want to do is when i save an entry it should fire up a method that based on the value of the content ( a specific field "Field1" ) of the saved entry ("Entry1") finds another entry ("Entry2") with another field ("Field2") whose value is same as Field1 and modifies the value of another field ("Field3") in Entry2 to reflect the value of Entry1's id.  
  > **username_0**: So as an example lets say I have two sections Books and Colors. Also each entry of Colors has a field of bookId, and each entry of Books has a field colorId. I save an entry of Colors with bookId 24 and it should automatically add the this newly saved color entry's id in the Book entry with id 24 (in the colorId field).  
  > **username_2**: Not being critical, just curious... What is the purpose behind each section (Books & Colors) having a reference to each others' ids? As in, why does Books have a colorId field **and** Colors has a bookId field? This isn't traditional database architecture... If it is a one-to-many relationship, then only one section needs to reference the other. If it is a many-to-many relationship, then the connecting data will be stored in the **craft_relations** table.  
  > **username_0**: I know it is not a traditional scheme but its like the person I am making this for wants to have a field in the color channel that he chooses himself to associate with books but he wants a field in the books channel to be automatically associated with the color channel.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2014-06-19. Score: 7 | Accepted answer
  
  You are correct in stating that Records typically define a new custom table for your plugin. However, don't forget that Craft already comes with many pre-existing Records.
  
  Take a look inside this folder:
  
  ```
  /craft/app/records
  
  ```
  
  Although based on your question (and the ensuing comments thread), it looks like you may not need to tap directly into any Records at all. I'd recommend you peruse the built-in API to see what may help you get the job done.
  
  Specifically, take a look at the EntriesService API...
  
  > <http://buildwithcraft.com/docs/apis/entriesservice>
  
  I suspect that everything you need to do can be done using those methods. After all, they already allow you to get, modify, and delete entry data.
  
  An important thing to note when dealing with the EntriesService API... You're not directly accessing Entry Records. Instead, you'll be dealing with Entry **Models**. This is greatly preferred, and you'll find that it's much easier to do what you want with a Model. (Behind the scenes, your model will tie into an Entry Record as needed.)
  
  ---
  
  On a separate note, it looks like you'll need to take advantage of **[Events](http://buildwithcraft.com/docs/plugins/hooks-and-events#events)**. You mentioned in a comment that you'd like an action to take place "when an entry is saved". To do that, you'll definitely want to take advantage of the **[entries.onSaveEntry](http://buildwithcraft.com/docs/plugins/events-reference#entries-onSaveEntry)** event.
  
  ---
  
  In summary, I don't think that you will need to create a custom Record, nor build your own custom Yii query.
  
  
  ---
  
  ### Answer by username_1 on 2014-06-19. Score: 2
  
  Craft is built on Yii, and comes with a [query builder](http://buildwithcraft.com/docs/plugins/database) for raw database access. It's based on [Yii's Query Builder](http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder) with some added functions.
  
  > **Comments:**
  > **username_0**: I can see that but before I create a database query I need to be pointed in the right direction of where i should create and execute it.  
  
  ---
  
  