Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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?

share|improve this question
    
Why would you need to edit the raw data, and not manipulate it via Services, Records, and Models? –  Bryan Redeagle Jun 19 at 2:55
    
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? –  user3309362 Jun 19 at 3:25
    
I would need more information about your plugin and what you wish to accomplish with it before I can give the most accurate answer. –  Bryan Redeagle Jun 19 at 4:14
    
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. –  user3309362 Jun 19 at 5:58
1  
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. –  Lindsey D Jun 19 at 7:18

2 Answers 2

up vote 6 down vote accepted

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. 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 event.


In summary, I don't think that you will need to create a custom Record, nor build your own custom Yii query.

share|improve this answer

Craft is built on Yii, and comes with a query builder for raw database access. It's based on Yii's Query Builder with some added functions.

share|improve this answer
    
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. –  user3309362 Jun 19 at 3:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.