不要怂,就是干,撸起袖子干!

fetchAssociations.ejs 3.75 KB
<script type="text/javascript" charset="utf-8">
  document.observe("dom:loaded", function() {
    buildNavigation([], ["<br />", "<a href=\"/examples/Count\">Count</a>",
"<a href=\"/examples/DefaultValues\">Default values</a>",
"<a href=\"/examples/fetchAssociations\">fetchAssociations</a>",
"<a href=\"/examples/MethodPassing\">MethodPassing</a>",
"<a href=\"/examples/Performance\">Performance</a>",
"<a href=\"/examples/SequelizeWithOptions\">SequelizeWithOptions</a>",
"<a href=\"/examples/ChainQueries\">Using the chainQueries function</a>",
"<a href=\"/examples/UsingMultipleModelFiles\">UsingMultipleModelFiles</a>",
"<a href=\"/examples/Associations\">Working with associations</a>"], { seperator: ' ' })
  })
</script>

<div>
  <a name="sequelize"></a>
  <h1>fetchAssociations</h1>
  <p></p>
</div>


  <div class="seperator"></div>

  <div>
    <a name="app.js"></a>
    <h2>app.js</h2>
    <p>
      
      <pre>var Sequelize = require(__dirname + &quot;/../../lib/sequelize/Sequelize&quot;).Sequelize,
    sequelize = new Sequelize(&quot;sequelize_test&quot;, &quot;root&quot;, null, {disableLogging: false})

var Person = sequelize.define('person', {
  name: Sequelize.STRING
})

var Pet = sequelize.define('pet', {
  name: Sequelize.STRING
})

Person.hasManyAndBelongsTo('pets', Pet, 'owner')

Sequelize.chainQueries([{drop: sequelize}, {sync: sequelize}], function() {
  var person  = new Person({ name: 'Luke' }),
      pet1    = new Pet({ name: 'Bob' }),
      pet2    = new Pet({ name: 'Aaron' })

  Sequelize.chainQueries([{save: person}, {save: pet1}, {save: pet2}], function() {
    person.setPets([pet1], function(pets) {

      Sequelize.Helper.log('my pet: ' + pets[0].name )
      Sequelize.Helper.log(&quot;Now let's get the same data with fetchData!&quot;)

      person.fetchAssociations(function(data) {
        Sequelize.Helper.log(&quot;And here we are: &quot; + data.pets[0].name)
        Sequelize.Helper.log(&quot;The object should now also contain the data: &quot; + person.fetchedAssociations.pets[0].name)

        Sequelize.Helper.log('This won\'t do a database request!')
        person.getPets(function(pets) {

          Sequelize.Helper.log(&quot;Pets: &quot; + pets.map(function(pet) { return pet.name }).join(&quot;, &quot;))
          Sequelize.Helper.log(&quot;Let's associate with another pet...&quot;)

          person.setPets([pet1, pet2], function() {

            Sequelize.Helper.log(&quot;The set call has stored the pets as associated data!&quot;)
            Sequelize.Helper.log(&quot;And now let's find the pets again! This will make no new database request but serve the already stored pets Bob and Aaron!&quot;)

            person.getPets(function(pets) {

              Sequelize.Helper.log(&quot;Pets: &quot; + pets.map(function(pet) { return pet.name }).join(&quot;, &quot;))
              Sequelize.Helper.log(&quot;Now let's force the reloading of pets!&quot;)

              person.getPets({refetchAssociations: true}, function(pets) {

                Sequelize.Helper.log(&quot;Pets: &quot; + pets.map(function(pet) { return pet.name }).join(&quot;, &quot;))

                Person.find(person.id, { fetchAssociations: true }, function(p) {
                  var petNames = p.fetchedAssociations.pets.map(function(pet) { return pet.name }).join(&quot;, &quot;)
                  Sequelize.Helper.log('Works with find as well: ' + petNames)
                })

                Person.findAll({ fetchAssociations: true }, function(people) {
                  var petNames = people[0].fetchedAssociations.pets.map(function(pet) { return pet.name }).join(&quot;, &quot;)
                  Sequelize.Helper.log('And also with findAll: ' + petNames)
                })

              })
            })
          })
        })
      })
    })
  })
})</pre>
    </p>
</div>